为什么执行其他任务后“ else”命令仍然运行?

时间:2018-10-25 22:41:19

标签: python if-statement

我的代码如下:

x = input('Enter Meal (Dinner, Desert, Entre, Breakfast, Lunch, or Purify water (how to purify it)): ')
if x == "Dinner":
    print('Ingredients: 1.5kg boneless rolled pork loin, 2L boiling water, Sea-salt flakes, to season, ¼ cup extra virgin olive oil, Roast vegetables and apple sauce, to serve. Method: Preheat oven to 230°C. Untie pork and use a small, sharp knife to cut fine parallel lines into skin (in the direction you want to carve it later). Take care not to cut into the meat.Put pork, skin-side up, on a wire rack set over the sink. Pour boiling water over skin and pat dry with paper towel. This will cause the skin to shrink, exposing the score lines.Roll up and secure with 3 lengths of butcher’s twine. Rub a generous amount of salt into skin, rubbing into scores. Drizzle with ½ the oil, then massage it into skin using your hands.Put pork on a wire rack in a shallow oven tray. Bake for 20 minutes. Remove from oven and drizzle with remaining oil. Reduce temperature to 150°C and bake for a further 1 hour.Remove pork from oven and baste with a little of the pan juices. Set aside, uncovered, to rest for 15 minutes. Increase oven to 220°C. Bake for a further 10 minutes.')
if x == "Desert":
    print('Ingredients: 50g butter, chopped, 1/3 cup (70g) brown sugar, 1/4 cup (60ml) golden syrup, 1/3 cup (50g) plain flour, 1 teaspoon Masterfoods Ground Ginger, 2/3 cup (160ml) thickened cream, 2 teaspoons brandy, 1 tablespoon icing sugar mixture. Method: Preheat oven to 180°C. Line a tray with baking paper. Combine the butter, brown sugar and golden syrup in a saucepan and cook over medium heat, stirring, for 2-3 minutes or until butter melts and sugar dissolves. Set pan aside for 5 minutes to cool slightly. Add the flour and ginger to the mixture and stir to combine. Drop four teaspoonfuls of mixture on to the tray, spaced well apart. Bake for 5 minutes or until they are bubbling and have spread to 10cm in diameter. Remove from oven and allow to cool for 1 minute. Working quickly, use a palette knife to lift each one and wrap around the handle of a wooden spoon. Leave to set for 1 minute, gently slip off spoon and transfer to a wire rack. Repeat with remaining mixture. Use an electric mixer to whisk the cream, brandy and icing sugar in a small bowl until soft peaks form. Spoon cream into a piping bag fitted with a fluted nozzle. Pipe the cream among each brandy snap. Serve immediately.')
if x == "Breakfast":
    print('Ingredients: 2   Eggs, 2 tbsp.   water, 1/8 tsp. salt, pinch of pepper, 1 tsp. butter, 1/3 cup   filling, such as shredded cheese, finely chopped ham. Instructions: Beat eggs, water, salt and pepper in small bowl until blended. Heat butter in 6 to 8-inch nonstick omelet pan or skillet over medium-high heat until hot. TILT pan to coat bottom. POUR IN egg mixture. Mixture should set immediately at edges. Gently puch cooked portions from edges toward the center with inverted turner so that uncooked eggs can reach the hot pan surface. Continue cooking, tilting pan and gently moving cooked portions as needed. When top surface of eggs is thickened and no visible liquid egg remains, place filling on one side of the omelet. Fold omelet in half with turner. With a quick flip of the wrist, turn pan and invert or slide omelet onto plate. Serve immediately.')
if x == "Entre":
    print("To cook garlic bread you will need garlic bread and an oven. You can get the garlic bread from a super market (frozen,) or you can make it yourself by purchasing a frozen loaf of crusty bread, evenly spacing out cuts in the bread and putting the garlic part in the garlic bread by mixing butter, chives and garlic together. Pre heat oven too 200c and put garlic bread in for 12 to 15 mins. Carefully remove and you got some garlic bread")
else:
    print('Did not understand request, please check spelling was correct and started with a capital letter.')

运行任务后,请说晚餐。即使在Thonny中,我仍然可以从else命令获得消息,即使它打印了晚餐任务。

为什么会这样?

2 个答案:

答案 0 :(得分:1)

else:块仅位于其前else语句的if处,因此,在这种情况下,因为您的输入x != "Entre",即if块不会被评估,并且代码直接跳转到else。如果您只希望else仅在if语句的 none 都被撤消为True时运行,则应该像这样链接elif

x = input('Enter Meal (Dinner, Desert, Entre, Breakfast, Lunch, or Purify water (how to purify it)): ')
if x == "Dinner":
    print('Ingredients: 1.5kg boneless rolled pork loin, 2L boiling water, Sea-salt flakes, to season, ¼ cup extra virgin olive oil, Roast vegetables and apple sauce, to serve. Method: Preheat oven to 230°C. Untie pork and use a small, sharp knife to cut fine parallel lines into skin (in the direction you want to carve it later). Take care not to cut into the meat.Put pork, skin-side up, on a wire rack set over the sink. Pour boiling water over skin and pat dry with paper towel. This will cause the skin to shrink, exposing the score lines.Roll up and secure with 3 lengths of butcher’s twine. Rub a generous amount of salt into skin, rubbing into scores. Drizzle with ½ the oil, then massage it into skin using your hands.Put pork on a wire rack in a shallow oven tray. Bake for 20 minutes. Remove from oven and drizzle with remaining oil. Reduce temperature to 150°C and bake for a further 1 hour.Remove pork from oven and baste with a little of the pan juices. Set aside, uncovered, to rest for 15 minutes. Increase oven to 220°C. Bake for a further 10 minutes.')
elif x == "Desert":
    print('Ingredients: 50g butter, chopped, 1/3 cup (70g) brown sugar, 1/4 cup (60ml) golden syrup, 1/3 cup (50g) plain flour, 1 teaspoon Masterfoods Ground Ginger, 2/3 cup (160ml) thickened cream, 2 teaspoons brandy, 1 tablespoon icing sugar mixture. Method: Preheat oven to 180°C. Line a tray with baking paper. Combine the butter, brown sugar and golden syrup in a saucepan and cook over medium heat, stirring, for 2-3 minutes or until butter melts and sugar dissolves. Set pan aside for 5 minutes to cool slightly. Add the flour and ginger to the mixture and stir to combine. Drop four teaspoonfuls of mixture on to the tray, spaced well apart. Bake for 5 minutes or until they are bubbling and have spread to 10cm in diameter. Remove from oven and allow to cool for 1 minute. Working quickly, use a palette knife to lift each one and wrap around the handle of a wooden spoon. Leave to set for 1 minute, gently slip off spoon and transfer to a wire rack. Repeat with remaining mixture. Use an electric mixer to whisk the cream, brandy and icing sugar in a small bowl until soft peaks form. Spoon cream into a piping bag fitted with a fluted nozzle. Pipe the cream among each brandy snap. Serve immediately.')
elif x == "Breakfast":
    print('Ingredients: 2   Eggs, 2 tbsp.   water, 1/8 tsp. salt, pinch of pepper, 1 tsp. butter, 1/3 cup   filling, such as shredded cheese, finely chopped ham. Instructions: Beat eggs, water, salt and pepper in small bowl until blended. Heat butter in 6 to 8-inch nonstick omelet pan or skillet over medium-high heat until hot. TILT pan to coat bottom. POUR IN egg mixture. Mixture should set immediately at edges. Gently puch cooked portions from edges toward the center with inverted turner so that uncooked eggs can reach the hot pan surface. Continue cooking, tilting pan and gently moving cooked portions as needed. When top surface of eggs is thickened and no visible liquid egg remains, place filling on one side of the omelet. Fold omelet in half with turner. With a quick flip of the wrist, turn pan and invert or slide omelet onto plate. Serve immediately.')
elif x == "Entre":
    print("To cook garlic bread you will need garlic bread and an oven. You can get the garlic bread from a super market (frozen,) or you can make it yourself by purchasing a frozen loaf of crusty bread, evenly spacing out cuts in the bread and putting the garlic part in the garlic bread by mixing butter, chives and garlic together. Pre heat oven too 200c and put garlic bread in for 12 to 15 mins. Carefully remove and you got some garlic bread")
else:
    print('Did not understand request, please check spelling was correct and started with a capital letter.')

我还建议lower()变量x,并检查文本的小写形式,即

if x.lower() == "dinner":

这样,用户可以输入"dinner""Dinner""DiNnEr",他们的求值结果均为"dinner"

答案 1 :(得分:0)

else仅考虑一系列elif和一个直接位于其前面的if语句。因此,您的else仅考虑"Entre"

尝试一下:

x = input('Enter Meal (Dinner, Desert, Entre, Breakfast, Lunch, or Purify water (how to purify it)): ')
if x == "Dinner":
    print('Ingredients: 1.5kg boneless rolled pork loin, 2L boiling water, Sea-salt flakes, to season, ¼ cup extra virgin olive oil, Roast vegetables and apple sauce, to serve. Method: Preheat oven to 230°C. Untie pork and use a small, sharp knife to cut fine parallel lines into skin (in the direction you want to carve it later). Take care not to cut into the meat.Put pork, skin-side up, on a wire rack set over the sink. Pour boiling water over skin and pat dry with paper towel. This will cause the skin to shrink, exposing the score lines.Roll up and secure with 3 lengths of butcher’s twine. Rub a generous amount of salt into skin, rubbing into scores. Drizzle with ½ the oil, then massage it into skin using your hands.Put pork on a wire rack in a shallow oven tray. Bake for 20 minutes. Remove from oven and drizzle with remaining oil. Reduce temperature to 150°C and bake for a further 1 hour.Remove pork from oven and baste with a little of the pan juices. Set aside, uncovered, to rest for 15 minutes. Increase oven to 220°C. Bake for a further 10 minutes.')
elif x == "Desert":
    print('Ingredients: 50g butter, chopped, 1/3 cup (70g) brown sugar, 1/4 cup (60ml) golden syrup, 1/3 cup (50g) plain flour, 1 teaspoon Masterfoods Ground Ginger, 2/3 cup (160ml) thickened cream, 2 teaspoons brandy, 1 tablespoon icing sugar mixture. Method: Preheat oven to 180°C. Line a tray with baking paper. Combine the butter, brown sugar and golden syrup in a saucepan and cook over medium heat, stirring, for 2-3 minutes or until butter melts and sugar dissolves. Set pan aside for 5 minutes to cool slightly. Add the flour and ginger to the mixture and stir to combine. Drop four teaspoonfuls of mixture on to the tray, spaced well apart. Bake for 5 minutes or until they are bubbling and have spread to 10cm in diameter. Remove from oven and allow to cool for 1 minute. Working quickly, use a palette knife to lift each one and wrap around the handle of a wooden spoon. Leave to set for 1 minute, gently slip off spoon and transfer to a wire rack. Repeat with remaining mixture. Use an electric mixer to whisk the cream, brandy and icing sugar in a small bowl until soft peaks form. Spoon cream into a piping bag fitted with a fluted nozzle. Pipe the cream among each brandy snap. Serve immediately.')
elif x == "Breakfast":
    print('Ingredients: 2   Eggs, 2 tbsp.   water, 1/8 tsp. salt, pinch of pepper, 1 tsp. butter, 1/3 cup   filling, such as shredded cheese, finely chopped ham. Instructions: Beat eggs, water, salt and pepper in small bowl until blended. Heat butter in 6 to 8-inch nonstick omelet pan or skillet over medium-high heat until hot. TILT pan to coat bottom. POUR IN egg mixture. Mixture should set immediately at edges. Gently puch cooked portions from edges toward the center with inverted turner so that uncooked eggs can reach the hot pan surface. Continue cooking, tilting pan and gently moving cooked portions as needed. When top surface of eggs is thickened and no visible liquid egg remains, place filling on one side of the omelet. Fold omelet in half with turner. With a quick flip of the wrist, turn pan and invert or slide omelet onto plate. Serve immediately.')
elif x == "Entre":
    print("To cook garlic bread you will need garlic bread and an oven. You can get the garlic bread from a super market (frozen,) or you can make it yourself by purchasing a frozen loaf of crusty bread, evenly spacing out cuts in the bread and putting the garlic part in the garlic bread by mixing butter, chives and garlic together. Pre heat oven too 200c and put garlic bread in for 12 to 15 mins. Carefully remove and you got some garlic bread")
else:
    print('Did not understand request, please check spelling was correct and started with a capital letter.')