[英]How to Connect User input to While Loop
我正在制作一个代码,用户应该选择一种肉来吃,然后它随机分配其他食物给 go 以完成一顿饭。 循环应该向您显示您拥有的物品,并且在循环结束时应该打印出总卡路里值。 我编写了整个程序,但是当我提示用户吃肉时,它会停止并且不会 go 进入 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)
我不确定问题是什么。 我知道它真的很小。 如果有人可以向我指出,我将不胜感激。 请显示代码中的更改。 提前致谢!
看看你的 while 循环的条件是什么。
while len(usermeal) == 4:
现在在 while 循环运行之前,运行它并查看它的输出
print(len(usermeal))
TL;DR 是您运行 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: #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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.