python实现自动售货机
本文实例为大家分享了python实现自动售货机的具体代码,供大家参考,具体内容如下
自动售货机代码
while True: n=input('请选择操作:') if n=='r': l=input('请输入货物数量:') v=l.split('-') numA1=int(v[0]) numA2=int(v[1]) numA3=int(v[2]) numA4=int(v[3]) numA5=int(v[4]) numA6=int(v[5]) t=input('请输入硬币数量:') u=t.split('-') yiyuan=int(u[0]) liangyuan=int(u[1]) wuyuan=int(u[2]) shiyuan=int(u[3]) print('S001:Initialization is successful') elif n=='q': print('会员管理'.center(50,'*')) print(""" 操作目录 0-货物剩余量 1-钱盒""") while True: c=input() if c=='0': print('A1 %d A2 %d A3 %d A4%d A5 %d A6 %d' %(numA1,numA2,numA3,numA4,numA5,numA6)) elif c=='1': print('1yuan coin number=%d 2yuan coin number=%d 5yuan coin number=%d 10 yuan coin number=%d' %(yiyuan,liangyuan,wuyuan,shiyuan)) elif n=='p': counname=input('请选择商品:') if counname=='1': if numA1<=0: print('E007:The goods sold out') else: m=input('请输入现金:') if m=='1': print('S003:Buy success,balance=1') print('E009:Work failure') numA1-=1 yiyuan+=1 elif m=='2': print('S003:Buy success,balance=1') numA1-=1 liangyuan+=1 yiyuan-=1 print('找您一元') elif m=='5': if yiyuan*1+liangyuan*2<5: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=1') print('S003:Buy success,balance=1') numA1-=1 wuyuan+=1 liangyuan-=2 print('找您4元') elif m=='10': if yiyuan*1+liangyuan*2<10: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=1') print('S003:Buy success,balance=1') shiyuan+=1 wuyuan-=1 liangyuan-=2 print('找您9元') else: print('E002:Denomination error') elif counname == '2': if numA2 <= 0: print('E007:The goods sold out') else: m=input('请输入现金:') if m == '1': print('E008:Lack of balance') elif m == '2': print('S003:Buy success,balance=1') print('E009:Work failure') numA2 -= 1 liangyuan += 1 elif m == '5': if yiyuan * 1 + liangyuan * 2 < 5: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=2') print('S003:Buy success,balance=1') wuyuan+=1 yiyuan-=1 liangyuan-=1 print('找您三元') elif m == '10': if yiyuan * 1 + liangyuan * 2 < 10: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=2') print('S003:Buy success,balance=1') numA2-=1 shiyuan+=1 wuyuan-=1 yiyuan-=1 liangyuan-=2 print('找您8元') else: print('E002:Denomination error') elif counname == '3': if numA3 <= 0: print('E007:The goods sold out') else: m=input('请输入现金:') if m == '1': print('E008:Lack of balance') elif m == '2': print('E008:Lack of balance') elif m == '5': if yiyuan * 1 + liangyuan * 2 < 5: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=3') print('S003:Buy success,balance=1') numA3-=1 wuyuan += 1 liangyuan -= 1 print('找您三元') elif m == '10': if yiyuan * 1 + liangyuan * 2 < 10: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=3') print('S003:Buy success,balance=1') numA3 -= 1 shiyuan += 1 wuyuan -= 1 liangyuan -= 1 print('找您7元') else: print('E002:Denomination error') elif counname == '4': if numA4 <= 0: print('E007:The goods sold out') else: m=input('请输入现金:') if m == '1': print('E008:Lack of balance') elif m == '2': print('E008:Lack of balance') elif m == '5': if yiyuan * 1 + liangyuan * 2 < 5: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=4') print('S003:Buy success,balance=1') numA4 -= 1 wuyuan += 1 yiyuan -= 1 print('找您1元') elif m == '10': if yiyuan * 1 + liangyuan * 2 < 10: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=4') print('S003:Buy success,balance=1') numA4 -= 1 shiyuan += 1 wuyuan -= 1 yiyuan -= 1 print('找您6元') else: print('E002:Denomination error') elif counname == '5': if numA5 <= 0: print('E007:The goods sold out') else: m=input('请输入现金:') if m == '1': print('E008:Lack of balance') elif m == '2': print('E008:Lack of balance') elif m == '5': print('E008:Lack of balance') elif m == '10': if yiyuan * 1 + liangyuan * 2 < 10: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=6') print('S003:Buy success,balance=1') numA5 -= 1 shiyuan += 1 liangyuan-=2 print('找您4元') else: print('E002:Denomination error') elif counname == '6': if numA6 <= 0: print('E007:The goods sold out') else: m=input('请输入现金:') if m == '1': print('E008:Lack of balance') elif m == '2': print('E008:Lack of balance') elif m == '5': print('S003:Buy success,balance=1') print('E009:Work failure') numA6 -= 1 wuyuan+=1 elif m == '10': if yiyuan * 1 + liangyuan * 2 < 10: print('E003:Change is not enough,pay fail') else: print('S002:Pay success,balance=5') print('S003:Buy success,balance=1') numA6 -= 1 shiyuan += 1 wuyuan -= 1 print('找您5元') else: print('E006:Goods does not exist') else: print('操作无效')
解释说明:钱币盒内只有,一元,两元,五元,十元的硬币;顾客也只能投入这些,钱币盒内一元和两元的总面额必须大于顾客给的面额,顾客一次只能投一次钱并买一件商品,但可以多次投入多次购买。货物数量和钱币数量由管理员初始化设定,管理员也可以查看钱币及货物数量,但查看完毕必须退出。如果投入钱币恰好等于商品价格,则购买成功并提示余额为0,工作失败,意味后续无法购买。
r——–初始化,必须初始化后才能执行程序
q——-管理员查看钱盒和货物信息
p——-顾客购买商品及付款及退钱
如图有:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
tensorflow:指定gpu 限制使用量百分比,设置最小使用量的实现
今天小编就为大家分享一篇tensorflow:指定gpu 限制使用量百分比,设置最小使用量的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-02-02关于django python manage.py startapp 应用名出错异常原因解析
这篇文章主要介绍了关于django python manage.py startapp 应用名出错异常原因解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12python利用蒙版抠图(使用PIL.Image和cv2)输出透明背景图
这篇文章主要介绍了python利用蒙版抠图(使用PIL.Image和cv2)输出透明背景图,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-08-08tensorflow之获取tensor的shape作为max_pool的ksize实例
今天小编就为大家分享一篇tensorflow之获取tensor的shape作为max_pool的ksize实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-01-01Python从使用线程到使用async/await的深入讲解
Python在3.5版本中引入了关于协程的语法糖async和await,所以下面这篇文章主要给大家介绍了关于Python从使用线程到使用async/await的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下2018-09-09
最新评论