goods = { 'A1': [2, 0], 'A2': [3, 0], 'A3': [4, 0], 'A4': [5, 0], 'A5': [8, 0], 'A6': [6, 0] } goods1 = { 'A1': [2, 0], 'A2': [3, 0], 'A3': [4, 0], 'A4': [5, 0], 'A5': [8, 0], 'A6': [6, 0] } moneys = { 1:0, 2:0, 5:0, 10:0, } money = 0 # 初始化 def initial(choice,goods,moneys): choice1 = choice[1].split('-') choice2 = choice[2].split('-') for i,v in zip(goods,range(6)): goods[i][1]=int(choice1[v]) for i,v in zip(moneys,range(4)): moneys[i]=int(choice2[v]) print("s001:Initialization is successful") # 投币 def payCoin(choice,goods,moneys): global money num = int(choice[1]) num1 = [1,2,5,10] if num not in num1: print("E002:Denomination error") elif moneys[1]+moneys[2]<num: print("E003:Change is not enough, pay fail") elif num>10: print("E004:Pay the balance is beyond the scope biggest") elif goods.values()==goods1.values(): print("E005:All the goods sold out") else: print("S002:Pay success,balance=%s" %(num)) moneys[num] += 1 money += num # 购买商品 def buyGoods(choice,goods): global money if choice[1] not in goods: print("E006:Goods does not exist") elif goods[choice[1]][1]==0: print("E007:The goods sold out") elif money<goods[choice[1]][0]: print("E008:Lack of balance") else: money = money-goods[choice[1]][0] goods[choice[1]][1]-=1 print("S003:Buy success,balance=%d" %(money)) # 退币 def returnCoin(moneys): global money if money ==0: print("E009:Work failure") else: change_money = {} while money: if moneys[10] and money >= 10: change_money[10] = change_money.get(10, 0) + 1 moneys[10] -= 1 money -= 10 elif moneys[5] and money >= 5: change_money[5] = change_money.get(5, 0) + 1 moneys[5] -= 1 money -= 5 elif moneys[2] and money >= 2: change_money[2] = change_money.get(2, 0) + 1 moneys[2] -= 1 money -= 2 elif moneys[1] and money >= 1: change_money[1] = change_money.get(1, 0) + 1 moneys[1] -= 1 money -= 1 else: break for i in moneys: print("%s yuan coin number=%s" %(i,change_money.get(i,0))) money=0 # 查询库存商品 def queryGoods(choice,goods,moneys): print(""" 查询类别 查询内容 0 查询商品信息 1 查询存钱盒信息 """) choice = int(input("请选择您要查询的类别:")) if choice == 0: li = sorted(goods.items(),key=lambda x:x[1][1]) for i,v in li: print("%s %s %s" %(i,v[0],v[1])) elif choice == 1: for i,v in moneys.items(): print("%s yuan xoin number=%s" %(i,v)) else: print("E010:Parameter error") def main(): while True: print("""**********请输入你需要的业务********** r. 初始化 p. 投币 b. 购买商品 c. 退币 q. 查询 """ ) choice = input("请您选择:").split() if choice[0] == 'r': initial(choice, goods, moneys) elif choice[0] == 'p': payCoin(choice, goods, moneys) elif choice[0] == 'b': buyGoods(choice, goods) elif choice[0] == 'c': returnCoin(moneys) elif choice[0] == 'q': queryGoods(choice,goods,moneys) else: break main()
自动售货系统
最新推荐文章于 2022-12-31 22:40:35 发布