匹配列表以决定然后循环,存储和显示

时间:2019-02-23 23:58:24

标签: python python-3.x

我正在尝试从列表S中提取一个值并将其与字典Cost匹配,我只需要帮助您掌握语法即可。它应该接受您的输入,将其检查到CostDict并输出V,然后它将检查是否可以购买5次,如果可以,则可以随机购买1-5个他们,检查是否有剩余的钱,并继续购买其中的1到5个或仅购买1个。但是我在此方面遇到了错误,这对我来说很混乱。

from collections import Counter


i = input('Taco, Burrito or Nacho: ')
moneyi = input('How much Money to Spend: ')
money = float(moneyi)

S = ["Taco", "Burrito", "Nacho"]
V = [6, 4, 2]
CostDict = {Li: Prop for (Li, Prop) in zip(S, V)}
cost = CostDict[i]
print("Costs:" + str(cost) + " per")
if (cost * 5) < money:
    purchase_ls = []
    while money >= min(cost):
        b = randint(1, 5)
        bcost = (CostDic[S] * b)
        bvalue = (ValueDic[S] * b)
        for aProp in S:
            if money < int(cost[S.index(aProp)]):
                continue
            purchase_ls.append(aProp)
            vcost = int(bcost[S.index(aProp)])
            money = money - bcost
            print("item: {} cost: {} money you have: {} ".format(aProp, vcost, money))
            if money <= 0:
                break
    print(dict(Counter(purchase_ls)))
else:
    purchase_ls = []
    while money >= min(cost):
        bcost = (CostDic[S])
        bvalue = (ValueDic[S])
        for aProp in S:
            if money < int(cost[S.index(aProp)]):
                continue
            purchase_ls.append(aProp)
            vcost = int(bcost[S.index(aProp)])
            money = money - bcost
            print("item: {} cost: {} money you have: {} ".format(aProp, vcost, money))
            if money <= 0:
                break
    print(dict(Counter(purchase_ls)))

0 个答案:

没有答案