python---自动售货机

goods = [
    ['农夫山泉', 2, 10],
    ['怡宝', 2, 10],
    ['冰红茶', 3, 10],
    ['可口可乐', 3, 10],
    ['脉动', 4, 10],
    ['红牛', 6, 10]
]
coins = [
    [1, 10],
    [2, 10],
    [5, 10],
    [10, 10]
]
balance = 0
GoodsName = []
for i in goods:  # 循环商品
    GoodsName.append(i[0])  # 添加商品名称

def sumMycoin(li,type=list):
    sum_1 = 0
    for i in li:
        sum_1 += i
    return sum_1
def sorted_by_count_price(x):
    return x[2],x[0]

#初始化
def initial():
    initialValue = input()
    initialValue_list = initialValue.split(" ")
    ##判断命令是否为'r'
    if initialValue_list[0] == 'r':
        ##切分商品数量钱币数量
        goods_count_list = initialValue_list[1].split("-")
        coins_count_list = initialValue_list[2].split("-")
    ##将输入的数量赋值给商品数量
    for i in range(6):
        if int(goods_count_list[i]) < 11 and int(goods_count_list[i]) >= 0:
            goods[i][2] = int(goods_count_list[i])
        else:
            if int(goods_count_list[i]) < 0:
                goods_count_list[i] = 0
            elif int(goods_count_list[i]) > 10:
                goods_count_list[i] = 10
            else:
                print("error")
    ##将输入的数量赋值给钱币数量
    for i in range(4):
        if int(coins_count_list[i]) < 11 and int(coins_count_list[i]) >= 0:
            coins[i][1] = int(coins_count_list[i])
        else:
            if int(coins_count_list[i]) < 0:
                coins_count_list[i] = 0
            elif int(coins_count_list[i]) > 10:
                coins_count_list[i] = 10
            else:
                print("error")
    print(goods, coins)
    print("S001:Initialization is successful")

#投币
coins_sum = []                                          ##投币
def Paycoin():
    global balance
    goods_count = 0                                     ##商品数量总数
    coins_values = [1,2,5,10]
    CoinValue = Command                                 ##用户投币输入
    CoinValue_list = CoinValue.split(" ")               ##分割输入
    if int(CoinValue_list[1]) in [1, 2, 5, 10]:     ##判断投币是否在[1,2,5,10]面额上
        balance = int(CoinValue_list[1])  ##用户投币的面额加入余额
        coins_sum.append(int(CoinValue_list[1]))
        coins[coins_values.index(int(CoinValue_list[1]))][1] += 1       ##对所投币的累加
        ##判断剩余1元面额与2元面额之和是否比投入面额小
        if coins[0][1] * 1 + coins[1][1] * 2 < balance :                   ##sumMycoin(coins_sum)
             #判断是否1,2元总数小于本次投入的面额
            print("E003:Change is enough,pay fail")
            coins_sum.pop()                                 #停止此次投币
        else:
            if balance > 10:
                print("E004:Pay the balance is beyond the scope biggest")
                coins_sum.pop()                             #停止此次投币
            else:
                for i in range(6):
                    goods_count += goods[i][2]      #商品总数判断
                if goods_count == 0:
                    print("E005:All the goods sold out")
                    coins_sum.pop()
                else:
                    print("S002:Pay success,balance=%d" %(sumMycoin(coins_sum)))
        # return sumMycoin(coins_sum)
    elif int(CoinValue_list[1]) not in [1, 2, 5, 10]:
        print("E002:Denomination error")


#购买
BuySpend = []
def buyGoods():     #b A1
    GoodsNeed = Command
    GoodsNeed_list = GoodsNeed.split(" ")
    print(GoodsNeed_list)
    for i in goods:     #循环商品
        if GoodsNeed_list[1] not in GoodsName:      ##如果输入商品不在商品名称列表里,则E006
            print(GoodsNeed_list[1])
            print(i)
            print("E006:Goods does not exist")
        elif GoodsNeed_list[1] == i[0]:             #输入商品在商品名称列表里,输出对应商品的相关信息
            GoodsInfo = i
    if GoodsInfo[2] == 0:
        print("E007:The goods sold out")
    else:
        if GoodsInfo[1] > sumMycoin(coins_sum):
            print("E008:Lack of balance")
        else:
            coins_sum.append(-GoodsInfo[1])
            BuySpend.append(GoodsInfo[1])
            print("S003:Buy success,balance=%d" % (sumMycoin(coins_sum)))

#退币
Coin_return = []
for i in range(4):
    Coin_return.append([coins[i][0],0])
def returnCoin():
    if sumMycoin(coins_sum) == 0:
        print("E009:Work failure")
    elif sumMycoin(coins_sum) > 0:
        for i in range(4)[::-1]:
            if coins[i][0] <= sumMycoin(coins_sum) and coins[i][1] > 0:
                coins[i][1] -= 1
                coins_sum.append(-coins[i][0])
                Coin_return[i][1] += 1
        for i in range(3):
            print("%d  yuan coin number=%d" %(Coin_return[i][0],Coin_return[i][1]))
        print("%d yuan coin number=%d" % (Coin_return[3][0], Coin_return[3][1]))



#查询
def queryGoods():
    goodsInfo = Command_list
    if goodsInfo[1] == '0':
        goodsSorted = sorted(goods,key = sorted_by_count_price)
        for i in goodsSorted:
            print("%s\t%d\t%d" %(i[0],i[1],i[2]))
    elif goodsInfo[1] == '1':
        for i in range(3):
            print("%d  yuan coin number=%d" %(coins[i][0],coins[i][1]))
        print("%d yuan coin number=%d" % (coins[3][0], coins[3][1]))
    else:
        print("E010:Parameter error")

initial()
#r 1-2-3-4-5-6 4-3-2-1
while True:
    Command = input()
    print(Command)
    Command_list = Command.split(" ")
    if Command_list[0] == 'p':
        Paycoin()
    elif Command_list[0] == 'b':
        buyGoods()
    elif Command_list[0] == 'c':
        returnCoin()
    elif Command_list[0] == 'q':
        queryGoods()

你可能感兴趣的:(python---自动售货机)