-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