-1
things = ["shorts", "pen", "pencil"]
userthings = input("what do you want to find?")
userthings = userthings.lower()
bagged = False
for i in things:
if i == userthings:
bagged = True
if bagged == True:
print ("I has thing")
else:
print ("I dont has thing")
morestuff = input("Do you want to add anything else?")
morestuff = morestuff.lower()
while (morestuff == "yes" or "y"):
stuff = input("What do you want to add?")
str(stuff)
things.append(stuff)
print ("I have the following things:", things)
morestuff = input("Do you want to add anything else?")
else:
print ("Lol bye")
我的问题是while语句代码,当我输入“无”到morestuff
,或任何其他比"yes"
或"y"
未打印"Lol bye"
但与进行stuff = input("What do you want to add?")
声明并进入无限循环。Python的 - 虽然声明不生产时虚假陈述
原因你的错误是,Python看到'morestuff == “是” 或 “y”'为'(morestuff == “是”)或(“y”)'。除非你输入“yes”,否则'(morestuff ==“yes”)'会评估为'False',而你留下'False'或'y'',评估为''y''。当'“y”'被视为布尔值时,它的计算结果为'True'。如果输入“yes”,则“True”或“y”也会评估为“True”。所以你的循环将永远持续下去。 – Galax
即使你尝试过'morestuff ==(“yes”或“y”)这样的东西,它仍然不能正常工作,因为“yes”或“y”评估为“yes”所以只有输入“yes”才会让你的代码继续。 – Galax