[英]How do I loop multiple conditions in different statements, and make it loop If the conditions are not met?
我需要专门针对我的代码的帮助。 output 似乎不是我想要的。 如果我尝试 while 循环它,并且值是错误的,它仍然会继续。 这里有很多错误,我不知道如何修复。
weapon_2 = "axe"
weapon_3 = "spiked club"
weapon_1 = weapon_1.strip().lower()
weapon_2 = weapon_1.strip().lower()
weapon_3 = weapon_1.strip().lower()
weapon = input ("Please select a weapon: Sword, Axe, Spiked Club.\n")
weapon = weapon.strip().lower()
if weapon == weapon_1:
print("Nice choice! I would pick {} too!".format(weapon))
elif weapon == weapon_2:
print("Nice choice! I would pick {} too!".format(weapon))
elif weapon == weapon_3:
print("Nice choice! I would pick {} too!".format(weapon))
while weapon != (weapon_1) or (weapon_2) or (weapon_3):
print("Invalid option.")
weapon = input ("Please select a weapon: Sword, Axe, Spiked Club.\n")
代替:
while weapon:= (weapon_1) or (weapon_2) or (weapon_3):
和:
while weapon not in (weapon_1, weapon_2, weapon_3):
or
正在将(weapon_2)
评估为 boolean,它看到一个str
因此评估为True
并且while
循环继续运行。
您还应该使打印语句中的选项在文本上与选项相同,否则用户将键入“Axe”,它与“axe”不匹配并被告知这是一个无效选项。
需要对 while 循环进行一些改进以保持“循环”
weapon_1, weapon_2, weapon_3 = "sword", "axe", "spiked club"
while True:
weapon = input("Please select a weapon: Sword, Axe, Spiked Club.\n").strip().lower()
if weapon == weapon_1:
print(f"Nice choice! I would pick {weapon_1} too!")
elif weapon == weapon_2:
print(f"Nice choice! I would pick {weapon_2} too!")
elif weapon == weapon_3:
print(f"Nice choice! I would pick {weapon_3} too!")
elif weapon == 'stop':
break
else:
print("Invalid Input")
weapons = ["sword", "axe", "spiked club"]
while True:
# input
user_input = input("Please select a weapon: Sword, Axe, Spiked Club. ('stop' to exit)\n")
user_input_lower = user_input.strip().lower()
if user_input_lower == 'stop':
break
# weapon precessing
weapon = user_input_lower
if weapon in weapons:
print(f"Nice choice! I would pick {user_input} too!")
else:
print("Invalid Input")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.