简体   繁体   中英

The code skips a command in Python. Can anyone tell me why?

So, basically, I was just coding so that if you write a game's name, it opens it.

print('You can play games here!')
time.sleep(1.5)
print('Snake')
time.sleep(0.5)
print('Maze')
time.sleep(1.5)
print('Type the name of one of the games to open it!')

game = input()

if game == 'Snake':
        import Snake
else:
    if game == 'Maze':
            import Maze

print('This Program Is Still In Development')

When I choose "Snake", it works perfectly. But when I choose "Maze", it just skips to the print('This Program Is Still In Development') part.

Can anyone help, please? I am on MAC OS X btw

EDIT: If I do elif , it doesn't change. Neither does a function. For some reason the only game that works is Snake.

EDIT 2: The weird thing is that when I just open Maze.py separately, IT WORKS. HOW?

change this:

else:
    if statement:
        # code

into this:

elif statement:
    # code

this works (added print to see its working ):

import time
print('You can play games here!')
time.sleep(1.5)
print('Snake')
time.sleep(1.5)
print('Maze')
time.sleep(1.5)

game = input('Type the name of one of the games to open it!')

# lowercase the user input to have less conditions
# and to make sure that we skip conditions because of case sensitivity
game = game.lower()

if game == 'snake':
   # do stuff
   print("snake")

elif game == 'maze':
   # do stuff
   print("maze")

print('This Program Is Still In Development')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM