【问题标题】:How to Connect User input to While Loop如何将用户输入连接到 While 循环
【发布时间】:2022-01-18 20:16:17
【问题描述】:

我正在编写一个代码,用户应该选择要吃的肉,然后随机分配其他食物来完成一顿饭。循环应该向您显示您拥有的物品,并且在循环结束时应该打印出总卡路里值。我编写了整个程序,但是当我提示用户吃肉时,它会停止并且不会进入 while 循环。这是程序:

import random

usermeal = []
meatcalories = 0
fruitcalories = 0
vegcalories = 0
desertcalories = 0

#defining a list of different foods...
meats = {"NY Strip": 300, "Shrimp": 100, "Lobster": 200, "Clams": 300, "Scallops": 200}
vegetables = {"Asparagus": 50, "Potato": 80, "Brocoli": 50, "Kale": 30, "Beans": 40}
fruits = {"Apple": 40, "Orange": 30, "Bananna":45, "Kiwi": 40, "Mango": 40}
deserts = {"Ice cream":300, "Brownie": 400, "Cookie": 200, "Chocolate":300, "Candy": 100}


print("We are going to make you a meal today. Each meal will have one item from each food category:")
print(meats)
print(vegetables)
print(fruits)
print(deserts)
print("Start by choosing one of the meats")
meatchoice = input("Please choose the meat that you want: ")


while len(usermeal) == 4:
    #prompt the user for their meat decision
    if meatchoice not in meats:
        print("Please choose a meat from the options!")
    meatchoice = input("Please choose the meat that you want: ")
    if meatchoice in meats:
        usermeal.append(meatchoice)
        meatcalories = meats.get(meatchoice)
        print("This is your meal so far", usermeal)

#randomly chooses a vegetable
    print("Now lets get the vegetable!")
    random_veg = random.choice(list(vegetables))
    print("The random vegetable is:", random_veg)
    usermeal.append(random_veg)
    vegcalories = vegetables.get(random_veg)
    print("This is your meal so far,", usermeal, "and its calorie count:", vegcalories)

#randomly chooses a fruit
    print("Now lets get the fruit!")
    random_fruit = random.choice(list(fruits))
    print("The random fruit is:", random_fruit)
    usermeal.append(random_fruit)
    fruitcalories = fruits.get(random_fruit)
    print("This is your meal so far,", usermeal, "and its calorie count:", fruitcalories)

#randomly chooses a desert
    print("Now lets get the desert!")
    random_desert = random.choice(list(deserts))
    print("The random desert is:", random_desert)
    usermeal.append(random_desert)
    desertcalories = deserts.get(random_desert)
    print("This is your meal so far,", usermeal, "and its calorie count:", desertcalories)

print("Now since we have all the foods, lets see how many calories it is")
totalcals = meatcalories + vegcalories + fruitcalories + desertcalories
print("Total Calorie Count: ", totalcals)

我不确定问题出在哪里。我知道它真的很小。如果有人可以向我指出,我将不胜感激。请显示代码中的更改。提前致谢!

【问题讨论】:

    标签: python list dictionary while-loop


    【解决方案1】:

    看看你的while循环的条件是什么。

    while len(usermeal) == 4:
    

    现在在 while 循环运行之前,运行它并查看它的输出

    print(len(usermeal))
    

    TL;DR 是您运行 while 循环的条件在遇到它时不正确,因此循环永远不会运行。

    【讨论】:

      【解决方案2】:
      import random
      
      usermeal = []
      meatcalories = 0
      fruitcalories = 0
      vegcalories = 0
      desertcalories = 0
      
      #defining a list of different foods...
      meats = {"NY Strip": 300, "Shrimp": 100, "Lobster": 200, "Clams": 300, "Scallops": 200}
      vegetables = {"Asparagus": 50, "Potato": 80, "Brocoli": 50, "Kale": 30, "Beans": 40}
      fruits = {"Apple": 40, "Orange": 30, "Bananna":45, "Kiwi": 40, "Mango": 40}
      deserts = {"Ice cream":300, "Brownie": 400, "Cookie": 200, "Chocolate":300, "Candy": 100}
      
      
      print("We are going to make you a meal today. Each meal will have one item from each food category:")
      print(meats)
      print(vegetables)
      print(fruits)
      print(deserts)
      print("Start by choosing one of the meats")
      meatchoice = input("Please choose the meat that you want: ")
      
      
      while len(usermeal) != 4:  #changed condition to NOT 4
          #prompt the user for their meat decision
          if meatchoice not in meats:
              print("Please choose a meat from the options!")
              meatchoice = input("Please choose the meat that you want: ")
          elif meatchoice in meats:  # also moved meatchoice within the if, and made this an elif
              usermeal.append(meatchoice)
              meatcalories = meats.get(meatchoice)
              print("This is your meal so far", usermeal)
      
      #randomly chooses a vegetable
          print("Now lets get the vegetable!")
          random_veg = random.choice(list(vegetables))
          print("The random vegetable is:", random_veg)
          usermeal.append(random_veg)
          vegcalories = vegetables.get(random_veg)
          print("This is your meal so far,", usermeal, "and its calorie count:", vegcalories)
      
      #randomly chooses a fruit
          print("Now lets get the fruit!")
          random_fruit = random.choice(list(fruits))
          print("The random fruit is:", random_fruit)
          usermeal.append(random_fruit)
          fruitcalories = fruits.get(random_fruit)
          print("This is your meal so far,", usermeal, "and its calorie count:", fruitcalories)
      
      #randomly chooses a desert
          print("Now lets get the desert!")
          random_desert = random.choice(list(deserts))
          print("The random desert is:", random_desert)
          usermeal.append(random_desert)
          desertcalories = deserts.get(random_desert)
          print("This is your meal so far,", usermeal, "and its calorie count:", desertcalories)
      
      print("Now since we have all the foods, lets see how many calories it is")
      totalcals = meatcalories + vegcalories + fruitcalories + desertcalories
      print("Total Calorie Count: ", totalcals)
      

      我还认为您可以将随机选择的所有内容移到 while 之外,并且只循环选择肉类的条件。而且我认为您可以将条件更改为while meatchoice not in meats。但是,它按原样工作。

      更新:

      所以如果你想改变逻辑,它会是这样的:

      import random
      
      usermeal = []
      meatcalories = 0
      fruitcalories = 0
      vegcalories = 0
      desertcalories = 0
      
      # defining a list of different foods...
      meats = {"NY Strip": 300, "Shrimp": 100,
               "Lobster": 200, "Clams": 300, "Scallops": 200}
      vegetables = {"Asparagus": 50, "Potato": 80,
                    "Brocoli": 50, "Kale": 30, "Beans": 40}
      fruits = {"Apple": 40, "Orange": 30, "Bananna": 45, "Kiwi": 40, "Mango": 40}
      deserts = {"Ice cream": 300, "Brownie": 400,
                 "Cookie": 200, "Chocolate": 300, "Candy": 100}
      
      
      print("We are going to make you a meal today. Each meal will have one item from each food category:")
      print(meats)
      print(vegetables)
      print(fruits)
      print(deserts)
      print("Start by choosing one of the meats")
      meatchoice = input("Please choose the meat that you want: ")
      
      
      while meatchoice not in meats:
          # prompt the user for their meat decision
          print("Please choose a meat from the options!")
          meatchoice = input("Please choose the meat that you want: ")
      
      usermeal.append(meatchoice)
      meatcalories = meats.get(meatchoice)
      print("This is your meal so far", usermeal)
      
      # randomly chooses a vegetable
      print("Now lets get the vegetable!")
      random_veg = random.choice(list(vegetables))
      print("The random vegetable is:", random_veg)
      usermeal.append(random_veg)
      vegcalories = vegetables.get(random_veg)
      print("This is your meal so far,", usermeal,
            "and its calorie count:", vegcalories)
      
      # randomly chooses a fruit
      print("Now lets get the fruit!")
      random_fruit = random.choice(list(fruits))
      print("The random fruit is:", random_fruit)
      usermeal.append(random_fruit)
      fruitcalories = fruits.get(random_fruit)
      print("This is your meal so far,", usermeal,
            "and its calorie count:", fruitcalories)
      
      # randomly chooses a desert
      print("Now lets get the desert!")
      random_desert = random.choice(list(deserts))
      print("The random desert is:", random_desert)
      usermeal.append(random_desert)
      desertcalories = deserts.get(random_desert)
      print("This is your meal so far,", usermeal,
            "and its calorie count:", desertcalories)
      
      print("Now since we have all the foods, lets see how many calories it is")
      totalcals = meatcalories + vegcalories + fruitcalories + desertcalories
      print("Total Calorie Count: ", totalcals)
      

      【讨论】:

        猜你喜欢
        • 2013-11-16
        • 2020-02-15
        • 2014-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 1970-01-01
        • 2017-01-27
        相关资源
        最近更新 更多