我正在尝试用Python创建一个食谱书程序,但食谱不会打印

问题描述 投票:0回答:4
print("What would you like to make?")

recipes=["Smash Burgers","Pizza","Fried Chicken"]

for recipe in recipes:
    print (recipe)

Choice= input(recipes)
if Choice==recipes [0]:
    print ("Potato Buns Ingredients,415 g bread flour,45 g potato flakes ,1 Tbsp instant yeast ,1 Tbsp kosher salt,2 Tbsp sugar,170 g water (75-80°F) ,110 g whole milk (75-80°F),2 eggs, room temperature  ,4 Tbsp unsalted butter, cubed + room temperature,Egg wash (see recipe below),Egg Wash Ingredients: ,1 egg ,1 egg yolk ,Pinch of salt ,Smash Burgers Ingredients: ,16 oz chuck, ground ,8 oz brisket, ground ,8 oz boneless short rib, ground ,As needed neutral oil ,To taste kosher salt, To taste freshly ground black pepper,American cheese,Potato buns, toasted,Butter lettuce ,Beefsteak tomato, sliced,White onion, thinly sliced,Bread and Butter pickles, sliced ,Special sauce (see recipe below),Special Sauce Ingredients:,½ cup mayo ,½ cup ketchup,⅓ cup mustard, ¼ onion, grated 1 garlic clove, grated ½ tsp hot paprika ,1 Tbsp relish")
    
    if Choice==recipes [1]:
        print("FOR THE DOUGH:270g or 1 1/8c water (86F/30C),30g or 2 Tbsp olive oil ,5g or 1 3/4 tsp instant yeast,35g 8 1/3 tsp sugar,430g or 3 1/2c ap flour (11.7% protein or similar),12g or 2 1/8 tsp salt. PIZZA SAUCE: 150g or 2/3c tomato paste,150g or 2/3c water, 1g or 1/8 tsp salt ,10g or 2 1/3 tsp sugar,10g or 2 1/4 tsp olive oil ,1/4 tsp dried basil ,1/4 tsp dried oregano,1g or 1/2 tsp black pepper,1g or 1/3 tsp garlic powder,1g or 1/2 tsp onion powder. TO BUILD AND BAKE THE PIZZA:200g or 2c full fat aged mozzarella, grated ,90g or less than 1c aged provolone, grated,finely ground cornmeal (for dusting),flour (for dusting),pinch of salt") 

else:
        print=input("Select a valid recipe. Or Would you like to make one?")

我创建了一个包含所有食物的列表,并使用 if 语句创建了一个打印出食谱的条件。我已经为 0 索引做过一次,但下一个索引不会打印,只会打印 else 语句上的内容

我尝试更改代码的格式(抱歉,如果很难阅读),但似乎没有任何帮助。

python list if-statement
4个回答
0
投票

在你的程序中,菜谱必须等于第一个菜谱,然后才判断第二个菜谱。也许你可以将代码更改为以下

print("What would you like to make?")

recipes = ["Smash Burgers", "Pizza", "Fried Chicken"]

for recipe in recipes :
    print(recipe)

Choice = input(recipes)
if Choice == recipes[0] :
    print(
        "Potato Buns Ingredients,415 g bread flour,45 g potato flakes ,1 Tbsp instant yeast ,1 Tbsp kosher salt,2 Tbsp sugar,170 g water (75-80°F) ,110 g whole milk (75-80°F),2 eggs, room temperature  ,4 Tbsp unsalted butter, cubed + room temperature,Egg wash (see recipe below),Egg Wash Ingredients: ,1 egg ,1 egg yolk ,Pinch of salt ,Smash Burgers Ingredients: ,16 oz chuck, ground ,8 oz brisket, ground ,8 oz boneless short rib, ground ,As needed neutral oil ,To taste kosher salt, To taste freshly ground black pepper,American cheese,Potato buns, toasted,Butter lettuce ,Beefsteak tomato, sliced,White onion, thinly sliced,Bread and Butter pickles, sliced ,Special sauce (see recipe below),Special Sauce Ingredients:,½ cup mayo ,½ cup ketchup,⅓ cup mustard, ¼ onion, grated 1 garlic clove, grated ½ tsp hot paprika ,1 Tbsp relish")

elif Choice == recipes[1] :
        print(
            "FOR THE DOUGH:270g or 1 1/8c water (86F/30C),30g or 2 Tbsp olive oil ,5g or 1 3/4 tsp instant yeast,35g 8 1/3 tsp sugar,430g or 3 1/2c ap flour (11.7% protein or similar),12g or 2 1/8 tsp salt. PIZZA SAUCE: 150g or 2/3c tomato paste,150g or 2/3c water, 1g or 1/8 tsp salt ,10g or 2 1/3 tsp sugar,10g or 2 1/4 tsp olive oil ,1/4 tsp dried basil ,1/4 tsp dried oregano,1g or 1/2 tsp black pepper,1g or 1/3 tsp garlic powder,1g or 1/2 tsp onion powder. TO BUILD AND BAKE THE PIZZA:200g or 2c full fat aged mozzarella, grated ,90g or less than 1c aged provolone, grated,finely ground cornmeal (for dusting),flour (for dusting),pinch of salt")

else :
    print = input("Select a valid recipe. Or Would you like to make one?")

0
投票

您可以使用 match/case 语句,因为它在您的情况下更具可读性并且更易于维护:

print("What would you like to make?")

recipes = ["Smash Burgers", "Pizza", "Fried Chicken"]

for recipe in recipes:
    print(recipe)

Choice = input(recipes)
match (Choice):
    case "Smash Burgers":
        print("recipe1")
    case "Pizza":
        print("recipe2")
    case "Fried Chicken":
        print("recipe3")
    case _:
        print(
            "Select a valid recipe. Or Would you like to make one?")

请记住,匹配/大小写是在 Python 3.10 中引入的,因此如果您安装了旧版本的 python,它将无法工作。


0
投票

这是因为你的缩进而发生的。条件

if Choice==recipes [1]
位于条件
if Choice==recipes [0]
之内。你应该永远记住,Python 中的缩进非常重要。像这样修改你的代码:

print("What would you like to make?")

recipes=["Smash Burgers","Pizza","Fried Chicken"]

for recipe in recipes:
    print (recipe)

Choice= input(recipes)
if Choice==recipes [0]:
    print ("Potato Buns Ingredients,415 g bread flour,45 g potato flakes ,1 Tbsp instant yeast ,1 Tbsp kosher salt,2 Tbsp sugar,170 g water (75-80°F) ,110 g whole milk (75-80°F),2 eggs, room temperature  ,4 Tbsp unsalted butter, cubed + room temperature,Egg wash (see recipe below),Egg Wash Ingredients: ,1 egg ,1 egg yolk ,Pinch of salt ,Smash Burgers Ingredients: ,16 oz chuck, ground ,8 oz brisket, ground ,8 oz boneless short rib, ground ,As needed neutral oil ,To taste kosher salt, To taste freshly ground black pepper,American cheese,Potato buns, toasted,Butter lettuce ,Beefsteak tomato, sliced,White onion, thinly sliced,Bread and Butter pickles, sliced ,Special sauce (see recipe below),Special Sauce Ingredients:,½ cup mayo ,½ cup ketchup,⅓ cup mustard, ¼ onion, grated 1 garlic clove, grated ½ tsp hot paprika ,1 Tbsp relish")
    
elif Choice==recipes [1]:
    print("FOR THE DOUGH:270g or 1 1/8c water (86F/30C),30g or 2 Tbsp olive oil ,5g or 1 3/4 tsp instant yeast,35g 8 1/3 tsp sugar,430g or 3 1/2c ap flour (11.7% protein or similar),12g or 2 1/8 tsp salt. PIZZA SAUCE: 150g or 2/3c tomato paste,150g or 2/3c water, 1g or 1/8 tsp salt ,10g or 2 1/3 tsp sugar,10g or 2 1/4 tsp olive oil ,1/4 tsp dried basil ,1/4 tsp dried oregano,1g or 1/2 tsp black pepper,1g or 1/3 tsp garlic powder,1g or 1/2 tsp onion powder. TO BUILD AND BAKE THE PIZZA:200g or 2c full fat aged mozzarella, grated ,90g or less than 1c aged provolone, grated,finely ground cornmeal (for dusting),flour (for dusting),pinch of salt") 

else:
    print=input("Select a valid recipe. Or Would you like to make one?")

在此更新版本中,删除了

if Choice==recipes [1]
条件中的缩进,并将条件语句更改为
elif Choice==recipes [1]


0
投票
  1. 将 500 克面粉、10 克酵母和 300 毫升水放入碗中混合。
  2. 将面团揉10分钟。
  3. 加入3克盐。
  4. 醒发2小时。
  5. 200摄氏度烘烤30分钟。
© www.soinside.com 2019 - 2024. All rights reserved.