+ 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

9th Nov 2018, 1:32 PM
Ian Meesters
Ian Meesters - avatar
2 odpowiedzi
+ 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' ...
9th Nov 2018, 2:00 PM
Mert Yazıcı
Mert Yazıcı - avatar
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")
9th Nov 2018, 6:15 PM
Ian Meesters
Ian Meesters - avatar