Python-有没有办法提出相同系列的问题并存储每组答案直到用户完成?

作为一名非常新的Python编写者,我正在制作一个计算器,该计算器应该询问用户一系列问题,然后确定他们一生中花在筷子上的大约总金额。我目前正在尝试循环一系列问题,以便可以将答案用于计算总数。

while brand != ''
   brand = str(input('Enter a brand of chapstick you use: '))
   brand = brand.title()
   brand_cost = float(input('How much does', brand, 'chapstick cost? $'))
   brand_quantity = int(input('How many', brand, 'chapstick(s) do you own? '))
   brand_regularly = int(input('How many', brand, 'chapsticks do you finish per year? '))
   brand_since = int(input('At what age did you first buy', brand, 'Chapstick? '))
评论
  • uqui
    uqui 回复

    您可以将所有值附加到列表中,以便在使用完输入值后最后计算它们。如下所示:

    prices = []
    quantity = []
    per_year = []
    since = []
    
    while True:
        brand = str(input('Enter a brand of chapstick you use: '))
        brand = brand.title()
        brand_cost = float(input('How much does ' + brand + ' chapstick cost? $'))
        brand_quantity = int(input('How many ' + brand + ' chapstick(s) do you own?'))
        brand_regularly = int(input('How many' + brand + ' chapsticks do you finish per year? '))
        brand_since = int(input('At what age did you first buy' + brand + 'Chapstick? '))
        prices.append(brand_cost)
        quantity.append(brand_quantity)
        per_year.append(brand_regularly)
        since.append(brand_since)
        is_complete = str(input('Is that all (y/n)'))
        if is_complete == 'y':
            break
        elif is_complete == 'n'
            continue
    
    

    完成用户操作后,将有4个列表,其中包含可用于进行计算的所有输入值。