Python Forum

Full Version: Clean up Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just looking for suggestions on how to clean up my code.

This is my first program ever in python (aside from 'hello world' and helping my daughter make stuff in minecraft).

Any time and suggestions are greatly appreciated as I am trying to teach myself how to program/code.


# Create an inventory list to fill with items later
inv = []

# Create variable that need to change value to be successful
SE1 = 0 # dime
SE2 = 0 # razor
SE3 = 0 # shave cream
SE4 = 0 # flashlight
SE5 = 0 # key
SE6 = 0 # cover camera
SE7 = 0 # open security panel
SE8 = 0 # cut wires
SE9 = 0 # unlock door


# All the choices available for room 1
r1_choices = ["look around", "get inventory", "look in desk", "open top drawer",
    "get dime", "open middle drawer", "get razor", "get shave cream",
    "open bottom drawer", "get flashlight", "look at plant", "move plant",
    "move pot", "get key", "look at security camera", "spray security camera",
    "open security panel", "cut wires", "unlock door",
    "leave room", "help", "yes Help me"]

# Create a function to call the inventory list
def get_inv ():
   print ("\n")
   print ("Your inventory is now:")
   print (inv)

# Create function for 'Look Around' in Room 1
def room1 ():
    print ("""

   You are in a room. In the Southwest corner is a desk (D) with
   three side drawers. In the Northwest corner is a plant (P) in
   a pot.  In the Northeast corner is a security camera (C). In
   the middle of the East wall is a door (/) with a security panel (S)
   to the right of the door.

   ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
   ("| P                        C |")
   ("|                            |")
   ("|                            |")
   ("|                            |")
   ("|                            /")
   ("|                            /")
   ("|                           S|")
   ("|                            |")
   ("|                            |")
   ("| D                          |")
   ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

   """)

# Start interaction for game
print ("Welcome to Escape")

print ("Welcome to Escape from Ysert!")

# Give the player the start area
room1()

r1_choice = ""

while r1_choice != "leave room":
   print ("What do you wish to do? ")
   r1_choice = str(input())
   if r1_choice == r1_choices [0]:
       room1()
   elif r1_choice == r1_choices [1]:
       get_inv()
   elif r1_choice == r1_choices [2]:
       print (""" \n
       The desk in the Southwest corner has three drawers:
       Top, Bottom and Middle.
       \n """)
   elif r1_choice == r1_choices [3]:
       print ("\n")
       print ("""
       You open the drawer and see a dime
       """)
   elif r1_choice == r1_choices [4]:
       print ("\n")
       print ("""
       You take the dime from the drawer and close it
       """)
       inv.append ("dime")
       SE1 = 1
   elif r1_choice == r1_choices [5]:
       print ("\n")
       print ("""
       You open the drawer and see a razor and some shave cream
       """)
   elif r1_choice == r1_choices [6]:
       print ("\n")
       print ("""
       You take the razor from the drawer and close it
       """)
       inv.append ("razor")
       SE2 = 1
   elif r1_choice == r1_choices [7]:
       print ("\n")
       print ("""
       You take the shave cream from the drawer and close it
       """)
       inv.append ("shave cream")
       SE3 = 1
   elif r1_choice == r1_choices [8]:
       print ("\n")
       print ("""
       You open the drawer and see a flashlight
       """)
   elif r1_choice == r1_choices [9]:
       print ("\n")
       print ("""
       You take the flashlight from the drawer and close it
       """)
       inv.append ("flashlight")
       SE4 = 1
   elif r1_choice == r1_choices [10]:
       print ("\n")
       print ("""
       The plant is in a pot, the pot could be moved
       to see what is under the pot.
       """)
   elif r1_choice == r1_choices [11]:
       print ("\n")
       print ("""
       You see a key.
       """)
   elif r1_choice == r1_choices [12]:
       print ("\n")
       print ("""
       You see a key.
       """)
   elif r1_choice == r1_choices [13]:
       print ("\n")
       print ("""
       You take the key.
       """)
       inv.append ("key")
       SE5 = 1
   elif r1_choice == r1_choices [14]:
       print ("\n")
       print ("""
       The security camera is pointed toward the center of the room.
       I bet the lens could be covered with something.
       """)
   elif r1_choice == r1_choices [15]:
       if SE3 == 1:
           print ("\n")
           print ("""
           You spreay the camera lens with the shave cream.
           """)
           SE6 = 1
       else:
           print ("\n")
           print ("""
           You do not have the shave cream.
           """)
   elif r1_choice == r1_choices [16]:
       if SE1 == 1:
           print ("\n")
           print ("""
           You open the security panel and see two wires
           """)
           SE7 = 1
       else:
           print ("\n")
           print ("""
           You do not have a dime to turn the screws.
           """)
   elif r1_choice == r1_choices [17]:
       if SE7 == 1 and SE2 == 1:
           print ("\n")
           print ("""
           You cut the wires with the razor.
           """)
           SE8 = 1
       else:
           print ("\n")
           print ("""
           You do not have a razor or need to open the panel
           """)
   elif r1_choice == r1_choices [18]:
       if SE5 == 1:
           print ("\n")
           print ("""
           You unlock the door
           """)
           SE9 = 1
       else:
           print ("""
           You do dot have the key
           """)
   elif r1_choice == r1_choices[-3]:
       if SE9 == 1:
           print ("\n")
           print ("""
           You exit the room and enter a hallway.
           """)
       else:
           print ("\n")
           print ("""
           The door is locked.
           """)
   elif r1_choice == r1_choices [-2]:
       print ("\n")
       print ("""
       If you really want help, type 'yes Help me'
       """)
   elif r1_choice == r1_choices [-1]:
       print ("\n")
       print ("""
       Your choices are:
       (r1_choices)
       """)
   else:
       print ("""
       Not a valid selection.
       Type your response in all lowercase
       Try words like 'look at', 'look in', 'move', 'open', 'spray',
       'unlock', or 'cut'
       """)

TSE = SE1 + SE2 + SE3 + SE4 + SE5 + SE6 + SE7 + SE8 + SE9 # success or fail

if TSE == 9:
   print ("\n")
   print ("""
   YOU ESCAPED!
   """)
else:
   print ("\n")
   print ("""
   You attempted to escape, but you got caught.
   """)
functions, functions and functions. Each of the actions on a choice should be a function. And then you replace your long if/elif with a dictionary:

r1_choices = {'look around':room1, 'get inventory':get_inv}

choice = input()
if choice not in r1_choices:
    print("bad choice")
else:
    r1_choices[choice]() # calls the function associated with this choice
And when you add a room2, this is just a second dictionary, and perhaps you can create a dictionary of dictionaries where the keys are "room1", "room2"...