自动售货机

goods = {
    'A1': [2, 6],
    'A2': [3, 6],
    'A3': [4, 6],
    'A4': [5, 6],
    'A5': [8, 6],
    'A6': [6, 6]
}

goods1 =  {
    'A1': [2, 0],
    'A2': [3, 0],
    'A3': [4, 0],
    'A4': [5, 0],
    'A5': [8, 0],
    'A6': [6, 0]
}

moneys = {
    1:10,
    2:10,
    5:10,
    10:10,
}
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()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值