本文实例为大家分享了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——-顾客购买商品及付款及退钱
如图有:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。