+ 1
Fix my While loop.
Hey guy, i can't seem to get my loop to work. could someone tell me why this is wrong?? i'm trying to make a text based adventure game (for practicing python) but i seem to be stuck already haha #text based adventure, While Test. PLAYER_CHOICE_NAME = "undefined" while PLAYER_CHOICE_NAME !="yes" or "Yes" or "Ja" or "ja": PLAYER_NAME = input("what's your name? > ") PLAYER_CHOICE_NAME = input("you're name is now '" + PLAYER_NAME + "', do you accept? > ") if PLAYER_CHOICE_NAME != "yes" or "Yes" or "Ja" or "ja": print("okay lets start over") if PLAYER_CHOICE_NAME == "yes" or "Yes" or "Ja" or "ja": print("Very well, this is the adventure of " + PLAYER_NAME) break
3 ответов
+ 7
In Python string bool value is True if string is not empty
so your condition is always true because
name != 'yes' or 'Yes' or 'Ja' or 'ja'
is equivalent to
name != "yes" or True or True or True
you can fix it with "in" operator
name not in ('yes', 'Yes', 'ja', 'Ja')
and you can make it shorter using "str.lower" method
name.lower() not in ('yes', 'ja')
If you don't want to use in, you must use multiple != operators like this
name != 'yes' or name != 'Yes' ...
0
Ah thanks dude! i got it working perfectly now!
PLAYER_CHOICE_NAME = "undefined"
while PLAYER_CHOICE_NAME not in("yes", "Yes","Ja","ja"):
PLAYER_NAME = input("what's your name? > ")
PLAYER_CHOICE_NAME = input("you're name is now '" + PLAYER_NAME + "', do you accept? > ")
if PLAYER_CHOICE_NAME in("yes", "Yes", "Ja", "ja"):
print("Very well, this is the adventure of " + PLAYER_NAME)
break
print("okay, lets start over")
0
Hey Ian, don’t know if you remember me but I’m Soddit from WoW, we played together a long time ago and you visited me in England, I’d love to connect with you again, my instagram is ohsoddit