从列表中拉出并加在一起的循环

时间:2019-02-23 16:57:45

标签: python python-3.x

我的程序即将完成(我认为),而我的最后一部分是创建一个更好的循环,该循环将获取列表并按顺序从列表中购买项目,直到用完为止。购买时,它将值加在一起,然后像

一样显示
import random

money = input('Total money: ')
mi = int(money)

Food = ['Taco', 'Burrito', 'Nacho']
Price = [6, 4, 2]
Dict = {f:p for (f, p) in zip(Food, Price)}

while mi >= 0:
    RFS = random.sample(Food, 1)[0]
    cost = Dict[RFS]
    mi = mi - cost
    print("item: {} cost: {} money you have: {} ".format(RFS,cost,mi))
  • 炸玉米饼x2
  • 墨西哥玉米煎饼x3
  • Nacho x1

3 个答案:

答案 0 :(得分:0)

您可以添加一个词典,其中保留了所购买产品的总数,然后,当没有足够的钱来购买任何其他物品时,它将被转换为“更改”。

import random

money = input('Total money: ')
mi = int(money)

Food = ['Taco', 'Burrito', 'Nacho']
Price = [6, 4, 2]
Dict = {f:p for (f, p) in zip(Food, Price)}

totals = {'Taco': 0, 'Burrito': 0, 'Nacho': 0, 'Change': 0}

while mi > 0:
    RFS = random.sample(Food, 1)[0]
    cost = Dict[RFS]
    if cost <= mi:
        mi += - cost
        print("item: {} cost: {} money you have: {} ".format(RFS, cost, mi))
        totals[RFS] += 1
    elif all(i > mi for i in Price):
        totals['Change'] = mi
        break

print(totals)

示例输出

Total money: 11
item: Nacho cost: 2 money you have: 9 
item: Taco cost: 6 money you have: 3 
item: Nacho cost: 2 money you have: 1 
{'Taco': 1, 'Burrito': 0, 'Nacho': 2, 'Change': 1}

答案 1 :(得分:0)

我认为您不希望随机变量成为代码的一部分,而是希望它是顺序的。以下代码应该可以满足您的要求。

from collections import Counter
money = input('Total money: ')
mi = int(money)
Food = ['Taco', 'Burrito', 'Nacho']
Price = [6, 4, 2]
#Dict = {f:p for (f, p) in zip(Food, Price)}
purchase_ls = []
while mi >= min(Price):    
    for aFood in Food:
        if mi < int(Price[Food.index(aFood)]):
            continue
        purchase_ls.append(aFood)
        cost = int(Price[Food.index(aFood)])
        mi = mi - cost
        print("item: {} cost: {} money you have: {} ".format(aFood,cost,mi))  
        if mi <= 0:
            break         
print(dict(Counter(purchase_ls)))

输出:

Total money: 22
item: Taco cost: 6 money you have: 16 
item: Burrito cost: 4 money you have: 12 
item: Nacho cost: 2 money you have: 10 
item: Taco cost: 6 money you have: 4 
item: Burrito cost: 4 money you have: 0 
{'Taco': 2, 'Burrito': 2, 'Nacho': 1}

Total money: 20
item: Taco cost: 6 money you have: 14 
item: Burrito cost: 4 money you have: 10 
item: Nacho cost: 2 money you have: 8 
item: Taco cost: 6 money you have: 2 
item: Nacho cost: 2 money you have: 0 
{'Taco': 2, 'Burrito': 1, 'Nacho': 2}

Total money: 10
item: Taco cost: 6 money you have: 4 
item: Burrito cost: 4 money you have: 0 
{'Taco': 1, 'Burrito': 1}

答案 2 :(得分:-1)

我用循环编写了一个简单的代码来完成您所要求的行为。代码是:

import random

money = int(input('Total money: '))

Food = ['Taco', 'Burrito', 'Nacho']
Price = [6, 4, 2]
Dict = {f:p for (f, p) in zip(Food, Price)}
Purchases = {f:0 for f in Dict}

stop = False
while not stop:
    for i in Dict:
        cost = Dict[i]
        if money - cost < 0:
            stop = True
        else:
            money -= cost
            Purchases[i] += 1

print(Purchases)

在代码中,我创建了一个名为Purchases的新字典,该字典保存Dict中每个元素的总购买次数。我使用了两个循环,外部的while循环一直运行到钱用光为止,内部的for循环依次遍历Dict的元素。我还更改了检查行为如果金额变为负数,用户将无法购买商品,此时循环停止。输出为字典,格式为

{'Taco': 1, 'Burrito': 0, 'Nacho': 0}

所以我相信您可以编写自己的打印循环以所需的方式打印输出。