我是代码方面的初学者。我已经为作业分配了此代码(这是节略的版本),但是每当我按下播放并开始该过程时,无论我输入多少侧,它总是会要求一定数量的平行侧,然后猜测并等腰三角形。为什么会这样?
print("Shape Guesser Game \n-----------------")
print("Welcome to the Shape Guesser Game! \nI believe that I can guess the shape you think of.")
print("There is only 1 rule: \nYou can only think of a polygon with a maximum of 6 sides.")
response_1 = input("\nDo you want to play? \nPick Yes or No")
if response_1 == "Yes" or "yes":
print("Great! Let's get started! \n------------------")
response_2 = input("Have you thought of your shape? Yes/No")
if response_2 == "Yes" or "yes":
response_3 = input("How many sides does it have?")
if response_3 == "3" or "three":
response_4 = input("How many equal sides does it have?")
if response_4 == "2" or "two":
response_5 = input("Aha! It is an isosceles triangle! Am I right? Yes or No?")
if response_5 == "yes" or "Yes":
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
elif response_4 == "3" or "three":
response_6 = input("Aha! It is an equilateral triangle! Am I right? Yes or No?")
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
else:
print("Not valid:Error \n-------------")
elif response_3 == "4" or "four":
response_7 = input("How many parallel sides does it have?")
if response_7 == "two" or "2":
response_8 = input("Aha! It is a trapezium! Am I right? Yes or No?")
if response_8 == "yes" or "Yes":
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
if response_7 == "four" or "4":
response_9 = input("Aha! It is a parallelogram! Am I right? Yes or No?")
if response_9 == "yes" or "Yes":
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
elif response_2 == "No" or "no":
print("Okay no worries, I will give you more time!")
elif response_1 == "No" or "no":
print("Sad to see you go! Back to Home.")
答案 0 :(得分:1)
实际上,每个if语句中都存在相同的问题。当你写类似
if response_1 == "Yes" or "yes"
python将此解释为
if (response_1 == "Yes") or ("yes")
也就是说:如果变量response_1等于“是”,或者如果字符串“是”为真,则if语句运行。 “是”是事实,因此if语句始终运行。 (例如,有关真实性含义的更多信息,请参见here。
您可能想要的是检查变量response_1是否等于“ Yes”或等于“ yes”,写为:
if response_1 == "Yes" or response_1 == "yes"`
当然,您还需要对其余的if语句应用相同的更正。
答案 1 :(得分:0)
使用此:
print("Shape Guesser Game \n-----------------")
print("Welcome to the Shape Guesser Game! \nI believe that I can guess the shape you think of.")
print("There is only 1 rule: \nYou can only think of a polygon with a maximum of 6 sides.")
response_1 = input("\nDo you want to play? \nPick Yes or No")
if response_1.lower() == "yes":
print("Great! Let's get started! \n------------------")
response_2 = input("Have you thought of your shape? Yes/No")
if response_2.lower() == "yes":
response_3 = input("How many sides does it have?")
if response_3 == "3" or response_3 == "three":
response_4 = input("How many equal sides does it have?")
if response_4 == "2" or response_4 == "two":
response_5 = input("Aha! It is an isosceles triangle! Am I right? Yes or No?")
if response_5.lower() == "yes":
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
elif response_4 == "3" or response_4 == "three":
response_6 = input("Aha! It is an equilateral triangle! Am I right? Yes or No?")
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
else:
print("Not valid:Error \n-------------")
elif response_3 == "4" or response_3 == "four":
response_7 = input("How many parallel sides does it have?")
if response_7 == "two" or response_7 == "2":
response_8 = input("Aha! It is a trapezium! Am I right? Yes or No?")
if response_8.lower() == "yes":
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
if response_7 == "four" or response_7 == "4":
response_9 = input("Aha! It is a parallelogram! Am I right? Yes or No?")
if response_9.lower() == "yes":
print("Yay I have succeeded! Thank you for playing this game. \nGo back to Home.")
elif response_2.lower() == "no":
print("Okay no worries, I will give you more time!")
elif response_1.lower() == "no":
print("Sad to see you go! Back to Home.")
or
不能像if a == 'b' or 'c':
那样工作,您必须使其if a == 'b' or a == 'c':
if response_1 == "Yes" or "yes"
之类的东西也可以变成if response_1.lower() == "yes"
,因此YeS
和yEs
也会被认为是'yes'
。