【问题标题】:Python - Is there a way to ask the same series of questions and store the each set of answers until the user is done?Python - 有没有办法提出同一系列问题并存储每组答案,直到用户完成?
【发布时间】:2020-05-23 23:09:06
【问题描述】:

作为一个非常新的 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? '))

【问题讨论】:

  • 我还应该补充一点,我知道示例的第一行中未定义“品牌”。我只需要明确表示我希望它也被定义多次。
  • 我认为在问这些问题之前,您需要自己进行一些研究。请查看我们的指南:how to ask?

标签: python loops input


【解决方案1】:

您可以将所有值附加到列表中,以便在使用完成输入他/​​她的值后计算它们。代码如下所示:

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 个列表,其中包含所有输入的值,您可以使用它们进行计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多