【问题标题】:Python - How do I get my program to ask for an input repeatedly but still store the other inputs?Python - 如何让我的程序重复请求输入但仍存储其他输入?
【发布时间】:2014-05-14 10:14:33
【问题描述】:

基本上,我的程序需要能够存储食谱。我必须要求用户输入成分以及数量和单位。如果这有点含糊,我很抱歉,但是我如何对其进行编程,以便我可以要求用户输入多种成分,直到他们输入了他们需要输入的成分数量?非常感谢。

#Ask the user to input ingredient name, quantity and measurement

ingredient = input("Name is your ingredient: ")
ingredientquantity = int(input("What is the quantity of this ingredient: "))
ingredientunit = input("What is the unit of your ingredient: ")

^^^ 这是我到目前为止所写的,但是如何重复并且STILL STORE任何以前的条目?

【问题讨论】:

    标签: python input store repeat


    【解决方案1】:

    编写一个循环,并将它们存储在一个列表中。 while 循环在这里可能是谨慎的

    ingredients = []
    while True:
        name = raw_input("Name is your ingredient: ")
        quantity = int(raw_input("What is the quantity of this ingredient: "))
        unit = raw_input("What is the unit of your ingredient: ")
        ingredients.append((name, quantity, unit))
    
        cont = raw_input("Continue adding ingredients? [y/n]")
        if not cont.lower() in ("y", "yes"):
            break
    

    代码会询问你想要的东西,并在每次迭代后将它们作为一个三元组附加到一个列表中。现在,一旦你完成(通过回答除 y 或 yes 之外的任何内容),你将拥有一个包含三个元组 (ingredient name, ingredient quantity, ingredient unit) 的列表。

    查看官方文档中的data structures部分。

    演示:

    Name is your ingredient: Flour
    What is the quantity of this ingredient: 7
    What is the unit of your ingredient: dl
    Continue adding ingredients? [y/n]y
    Name is your ingredient: Butter
    What is the quantity of this ingredient: 50
    What is the unit of your ingredient: gr
    Continue adding ingredients? [y/n]y
    Name is your ingredient: Sugar
    What is the quantity of this ingredient: 1
    What is the unit of your ingredient: dl
    Continue adding ingredients? [y/n]n
    
    >>> print ingredients
    [('Flour', 7, 'dl'), ('Butter', 50, 'gr'), ('Sugar', 1, 'dl')]
    

    编辑:将input 调用更改为raw_input,因为在python 2.7 中,input 尝试将输入转换为它认为的输入。因此数字变成整数等等。不一定是个好主意。

    【讨论】:

      【解决方案2】:
      def validated_input(prompt,input_type=str):
          while True:
             try:
                  return input_type(raw_input(prompt))
             except:
                  print "Error Invalid Input, Try Again"
      
      def get_ingredient():
          return validated_input("Ingredient Name:",str),validated_input("Qty:",int),validated_input("Units:",str)
      
      
      ingredients = [get_ingredient() for _ in range(validated_input("Enter Number Of Ingredients:",int))]
      

      【讨论】:

      • 我觉得这是更pythonic的答案,但为什么不使用raw_input代替input,所以在python 2.7中用户不必引用字符串?
      • 哦,我只是在复制 OP ...我不知道他的版本 :) ...更改为 raw_input(粗略的)
      • 还添加了一个快速而肮脏的输入验证器
      【解决方案3】:

      你可以试试这样的:

      data = []
      
      i=0
      max_cycles = 10
      while i < max_cycles:
      
          ingredient = input("Name is your ingredient: ")
          ingredientquantity = int(input("What is the quantity of this ingredient: "))
          ingredientunit = input("What is the unit of your ingredient: ")
          data.append((ingredient, ingredientquantity, ingredientunit))
      
          print(data)
      
          i += 1
      

      它将迭代 10 次(max_cycles 值)然后中断,并将所有数据作为元组存储在数据列表中。

      【讨论】:

        猜你喜欢
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 2018-04-18
        • 1970-01-01
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多