0

Very basic question but I'm new to Python and working on a small text-adventure game to help me better understand while loops along with if/else/elif. It worked well until I introduced while loops to the code. In the beginning the user is supposed to pick a door to walk into, right or left, if the user types something else it's supposed to back to the top and ask them to select their door. And it's doing that correctly but if you type in right or left like you're supposed to it still takes you back to ask you to select your door.

door1 = 'right'
door2 = 'left'
print("Please type the name you'd like to use")
myName = input()
print("Hello " + myName)
print("Would you like to go through the door on the left or the door on the right?")
while True:
    print("Please type left or right to indicate the door you choose")
    door = input()
    if door != door1 or door2:
        continue
Cavaz
  • 2,996
  • 24
  • 38
  • First of all, you can put your print statements inside input(). Secondly, there is no code after the the conditional...Since your loop condition is True, the loop will just keep running....you haven't told us(or your code) what you want to have happen when you select "left" or "right" – Albert Rothman Oct 05 '16 at 22:34
  • That actually solved my problem! I have code after that but didn't think it was relevant enough to add here, the issue was I didn't indent the code to be included in my loop so it just kept looping back rather than continuing on since it didn't know what to continue to. I didn't even think that it might be an issue until you said that. – Trevor Quinney Oct 05 '16 at 22:46
  • Glad to be of assistance, as you may have noticed, your question has been marked as a duplicate. If you haven't already, visit the site [tour](http://stackoverflow.com/tour) – Albert Rothman Oct 05 '16 at 22:51

1 Answers1

2

You probably mean:

if not door in (door1, door2):

What you have written is essentially:

if (door != door1) or bool(door2):

And bool(door2) == True unless door2 is the empty string.

Julien
  • 13,986
  • 5
  • 29
  • 53
cjhanks
  • 526
  • 4
  • 4