我想在if和elif语句中使用来自输入函数的单个字母,例如“ Y或y”和“ N或n”:
print("Main Breakfast Menu")
print("1. Pancakes")
print("2. Waffles")
print("3. Bacon & Eggs")
print("4. Omellette")
print("5. Oats")
print("6. Pizza Slice")
MainChoice = int(input("Select a Breakfast meal: "))
SideAns = input("Would you like a side to go with your meal? Y/N ")
if (MainChoice == 1) and (SideAns == Y):
print("Sides")
print("1. Extra Bacon")
print("2. Switch Bacon for Turkey Bacon")
print("3. Toast")
print("4. Syrup for pancakes or waffles")
print("5. Drinks")
下面的代码块在其中给出错误。 编译器指出未定义“ Y”之类的输入,当我传递“ y”或“ n”时,其状态也相同。
if (MainChoice == 1) and SideAns( == Y):
我在做什么错了?
答案 0 :(得分:1)
您缺少引号和缩进。
if (MainChoice == 1) and (SideAns == "Y"):
print("Sides")
您甚至可以使用:小写和大写都接受:
if (MainChoice == 1) and (SideAns.upper() == "Y"):
print("Sides")