python实现自动售货机
程序员文章站
2022-06-17 21:22:19
本文实例为大家分享了python实现自动售货机的具体代码,供大家参考,具体内容如下自动售货机代码while true: n=input('请选择操作:') if n=='r': l=input('...
本文实例为大家分享了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——-顾客购买商品及付款及退钱
如图有:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。