I'm new to Python and programming in general, but I'm making it work. I have this little game I've been working on as a learning exercise and I'm a bit concerned that I'm using far too many if/else statements for the user menus. Is there perhaps a better way?# Bizarro house text adventure gamefrom sys import exitimport time#error message for an incorrect choicedef error():    print     Invalid number    Time rewinds to the beginning of your journey.        start()#Beginning of the gamedef start():    print     You're standing at the entrance to an old spooky mansion.    It's just after midnight.    What would you like to do?    1. Go inside    2. leave        choice = raw_input(> )    if choice == 1:        entrance()    elif choice == 2:        dead(This could have been an exciting adventure if you weren't too scared,)    else:        print Wrong answer try again.        start()def dead(why):    print why, better luck next time.    exit(0)# Group of functions for the downstairs area    def entrance():    print     There is a room on the left,    a room on the right,    and a kitchen in the back.    Also a set of stairs    Which way?    1. Left    2. Right    3. Kitchen    4. Upstairs        choice = raw_input(> )    if choice == 1:        left_room()    elif choice == 2:        right_room()    elif choice == 3:        kitchen()    elif choice == 4:        stairwell()        else:        error()def left_room():    print     The room is dark.    You hear a voice that says:    'I see you, and so does Jesus'    You use your cell phone for light.    You shine the light around the room.     You find a lightswitch.    You also shine the light on a parrot cage.    The parrot repeats his warning.    'I see you and so does Jesus'    What do you do?    1. Turn on light    2. Leave room        choice = raw_input(> )    if choice == 1:        dead(You turn on the light and notice a huge Rottweiller. The parrot says 'Sick em Jesus' at least you were a tasty meal,)    elif choice == 2:        entrance()    else:        error()def right_room():    print     In the room you notice something floating in mid air.    Its a $100 bill.    What do you do?    1. Try to take the money    2. Leave the room        choice = raw_input(> )    if choice == 1:        cash()    elif choice == 2:        entrance()    else:        error()def cash():    print     You reach for the money.    A ghost in the room gives its poetic warning:    'I am the ghost of Daniel Boone,    take this money and you'll be doomed'    What will you do now?    1. Take the money anyway    2. Leave the room        choice = raw_input(> )    if choice == 1:        print          You reply with your own poetic quip:        'Yeah well I'm the ghost of Davy Crockett,        that money is going in my pocket'                dead(You never even touch the money, something hit you in the head and that was it,)                            elif choice == 2:        entrance()    else:        error()def kitchen():    print     Its seems to be a basic kitchen.                There's a refrigerator, a pantry,    and an oven.     It smeels like something may have been cooking recently.    You could use a snack.        kitchen_menu()def kitchen_menu():    print     What now?    1. Check pantry    2. Check refrigerator    3. Check oven    4. Leave room        choice = raw_input(> )    if choice == 1:        print You find a dead rat.        kitchen_menu()    elif choice == 2:        print Its empty.        kitchen_menu()    elif choice == 3:        dead(You open the oven and are immediately sucked into an inter-dimensional portal,)    elif choice == 4:        entrance()     else:        error()       #Functions for the upstairs areadef stairwell():    print     At the top of the stairwell you see three rooms.    1. A bedroom    2. A library    3. A recording studio    4. Back downstairs    Which way?        choice = raw_input(< )    if choice == 1:        bedroom()    elif choice == 2:        library()    elif choice == 3:        studio()    elif choice == '4':        entrance()        else:        error()def bedroom():    print     In the room is a very large kid.    He's 10 years old, 6'7, 290 lbs.    He's watching cartoons.    He tells you his mom said he could watch.    What do you do?    1. Turn off TV    2. Leave room    3. Ask if you can watch too.        choice = raw_input(> )    if choice == 1:        dead(The kid gets mad, picks you up and throws you through the second story window, without opening it,)    elif choice == 2:        stairwell()    elif choice == 3:        print You sit and watch for a while, then leave the room.        time.sleep(5)        stairwell()                                else:        error()def library():    print     Its a nice looking old library.    You notice one particular book.    Fear and Loathing in Las Vegas.    You've always wanted to read that.    What do you think?    1. Take the book off the shelf    2. Leave the room        choice = raw_input(> )    if choice == 1:        secret_room()    elif choice == 2:        stairwell()    else:        error()def secret_room():    print The bookshelf and floor spin around into a secret room.    time.sleep(5)    dead(In the secret room was a very hungry bear,)def studio():    print     You enter the studio and meet the owner of the house.    He turns out to be a very eccentric record producer.    He asks if you'd like to put down some vocals on his newest track.    What do you tell him?    1. Yes    2. No        choice = raw_input(> )    if choice == 1:        big_win()    elif choice == 2:            print         The producer gives you an angry scowl,        Then presses a button on his soundboard.        You are teleported to Sandusky Ohio's        annual meat parade.                dead(You are run over by the pork chop float,)    else:        error()    def big_win():    print You lay a vocal track.    time.sleep(3)    print It gets you signed to a major record label.    time.sleep(3)    print The song is a hit.    time.sleep(3)    print You make a fortune.    time.sleep(3)    print You have won this game.    exit(0)start()
Making user menus in a text-based game
python;beginner;game;console;adventure game
While a full object-oriented approach would work, I would go for a more lightweight solution that leveraged Python's native data structures. First, define all of your states like so:states = {  start: {    message: '\n'.join(You're standing at the entrance to an old spooky mansion.,                         It's just after midnight.,                         What would you like to do?),    options: [(Go inside, entrance), (Leave, dead)]  },  entrance: {...  }}Then you can handle all of the game logic in one loop:state = states['start']while True:  print state['message']  if state is states['dead']:    exit(0)  for i, opt in enumerate(state['options']):    print {}. {}.format(i+1, opt[0])  answer = raw_input(> )  if validate(answer, state['options']):    state = states[ state['options'][int(answer)-1][1] ]  else:    state = states['dead']validate() would just do basic checking (is it a number, is it actually an option in the list, etc.). I'll leave the body of that function to you.There are a few advantages to this approach. Compared to your original, it  operates within a single stack frame rather than creating a new frame for every new state. It also removes a lot of duplicate code. Compared to the fully object-oriented approach, it uses a lot less code, which means it's easier to maintain, has less potential bugs, less overhead, etc.The most significant advantage to this approach, though, is that it fully separates data from logic. You don't even have to touch the game code to change the adventure. In fact, you could store the adventure data in a serialization format (JSON and YAML are popular choices) and not even open the source code to make changes. If you worked in a game design company, you could pass off the adventure design to the artists/writers and dust your hands off, knowing you'd fully completed the engine with that single while-loop.