[英]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.