0

I decided to make a mini-game as my 1st program since I am very new to python. The user must enter yes to start the game if you type no the program is supposed to say "bye-bye" and end. if anything else is entered it print "I do not understand bye-bye" However when I enter no it plays the else statement along with what was supposed to play when it enters no. How do I fix this? This is my program (sorry if it's a little long). You can run it and type no to see what I mean.

light_on = False
door_open = False
print('hello')
start = input('Would you like to play a game yes or no ? ').lower() 
if start == 'no':
    print('bye bye')
while start == 'yes':
    print('Type help if you do not know how to play')
    command = input('Command?: ').lower()
    if command == 'help':
        print('''
on = turn light on
off = turn light off
open = open door
close = close door''')
    elif command == 'on':
        if light_on:
            print('The light is already on')
        else:
            light_on = True
            print('The light is bright and warming')
    elif command == 'off':
        if not light_on:
            print('the light is already off')
        else:
            light_on = False
            print('the room is dark and scary')
    elif command == 'open':
        if door_open:
            print('the door is already open!')
        else:
            door_open = True
            print('the hallway is so dark you cannot see')
    elif command == 'close':
        if not door_open:
            print('the door is already closed!')
        else:
            door_open = False
            print('you slammed the door shut')
    elif command == 'quit':
        print('bye bye')
        break
    else:
        print('I do not understand')
else:
    print('I do not understand bye bye')

1 Answer 1

1

The else: statement of a while loop is executed whenever the loop ends due to the condition failing, rather than being stopped by a break statement. Since no doesn't match the while start == 'yes': condition, the loop ends immediately, and the while is executed.

You should use elif rather than while for the yes option, then the final else will be connected to this if chain.

light_on = False
door_open = False
print('hello')
start = input('Would you like to play a game yes or no ? ').lower() 
if start == 'no':
    print('bye bye')
elif start == 'yes':
    print('Type help if you do not know how to play')
    command = input('Command?: ').lower()
    if command == 'help':
        print('''
on = turn light on
off = turn light off
open = open door
close = close door''')
    elif command == 'on':
        if light_on:
            print('The light is already on')
        else:
            light_on = True
            print('The light is bright and warming')
    elif command == 'off':
        if not light_on:
            print('the light is already off')
        else:
            light_on = False
            print('the room is dark and scary')
    elif command == 'open':
        if door_open:
            print('the door is already open!')
        else:
            door_open = True
            print('the hallway is so dark you cannot see')
    elif command == 'close':
        if not door_open:
            print('the door is already closed!')
        else:
            door_open = False
            print('you slammed the door shut')
    elif command == 'quit':
        print('bye bye')
        break
    else:
        print('I do not understand')
else:
    print('I do not understand bye bye')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.