I am new to coding and am working through Learn Python The Hard Way. I wrote a simple text game as instructed. The logic appears to be sound. I played through it and didn't run into any dead ends. I am simply looking for some pointers on how to make my code more neat, more readable, more efficient etc. I would like to avoid picking up bad habits in the early stage of my learning.
from sys import exit
key = False
def safe():
print "\tEnter three numbers to try the safe."
combination = raw_input("> ")
if combination == "10 21 15":
print """
Success! The safe opens. Inside, you
find a key and some cash.
"""
global key
key = True
room1Menu()
elif combination != "10 21 15":
print """
That didn't seem to work.
There must be a clue around
somewhere.
"""
room1Menu()
else:
dead("\tYou notice a strange substance on your fingers. You smell it and die instantly.")
def picture():
print """
You gaze at the picture, then inspect
it with your hands. You tug and the
edges and it opens to reveal an old
fashioned wall safe. It has a dial
with numbers that range from 0 to 50.
You remember from your high school locker
that these things require three numbers.
Enter '1' to continue to look around the room.
Enter '2' to try to open the safe.
"""
action = raw_input("> ")
if action == "1":
room1Menu()
elif action == "2":
safe()
else:
dead("\tYou notice a strange substance on your fingers. You smell it and die instantly.")
def dresser():
print """
The drawers are empty and there
are several useless objects on top.
There is a folded slip of paper.
You open it and it reads,
"Great Scott! It's the future today!"
"""
room1Menu()
def bed():
print "\tThere is nothing of interest here."
room1Menu()
def room1Menu():
print """
Enter '1' to investigate the dresser.
Enter '2' to investigate the bed.
Enter '3' to investigate the picture on the wall.
Enter '4' to exit the room.
"""
action = raw_input("> ")
if action == "1":
dresser()
elif action == "2":
bed()
elif action == "3":
picture()
elif action == "4":
hallway()
else:
dead("\tYou panic, have a heart attack and die.")
def room1():
print """
The room is sparse. There is
a dresser, a bed and a picture
on the wall depicting a mysterious
looking soldier.
"""
room1Menu()
def hallway():
print """
Enter '1' to try the door to the room inside.
Enter '2' to try the door that appears to lead outside.
"""
action = raw_input("> ")
if action == "1":
print "\tYou open the door and enter."
room1()
elif action == "2" and key == False:
print """
You try the door, but it is
locked from the outside. There
may be a key around somewhere...
"""
hallway()
elif action == "2" and key == True:
print """
You try the key and it fits!
You open the door to freedom
and live out your days wasting
time looking at memes on the
internet.
"""
exit(0)
else:
dead("\tYou pace in circles until you dehydrate and die.")
def dead(why):
print why, " Game Over."
exit(0)
def start():
print """
You suddenly awake in a dark hallway.
Your head hurts and you have no idea
how you got here.
Once your eyes adjust, you see that
there are two doors. One looks like
a door to another room, the other looks
like a door that leads outside.
"""
hallway()
start()