I've been teaching myself programming in Python for about three months now using How To Think Like a Computer Scientist and a bit of the internet with the intention of being able to create video games someday (nothing huge, but something/stuff worth playing). What I'm looking for is an assessment on how I'm doing and if I should change anything. Here's what I made with what I've learned. It's nothing much, but I'm pretty proud for coming this far:

# Right side of room dialogue:
box_see = '\nI see a box.'
box_q = 'I think I can open this. Should I?'
box_open = '\n*box opening sounds*'
box_easy = 'I opened the box with ease.'
crowbar_get = 'A crowbar? This will help.\n'
# Left side of room dialogue:
bookshelf = "\nThere's a bookshelf."
bookshelf_q = 'Should I move it?'
boarded_hole = '\nHuh, a boarded up hole...'
pry = "Let's try to pry this wood off with the crowbar."
mallet_get = 'A mallet? Should come in handy.\n'
# Back side of room dialogue:
crack = '\nI see something in a small crack on this wall.'
crack_q = 'Should I try to get it?'
crack_complementing = '\nMaybe if I. . .'
crack_smash = '*crash*'
key_get = "A key? This could be my ticket out of here!\n"
# General dialogue:
cant = "That won't work.\n"
no_dice = '\nNothing of interest here.\n'
leave_alone = "\nI'll leave it alone for now.\n"
hand = 1
crowbar = 0
mallet = 0
key = 0

def main_adventure():

def beginning():
    name = input('My name is...\n')
    if not name:
        name = 'Not important, I guess'
        name_line = ". . . {0}. (Press Enter to advance)".format(name)
        name_line = "it's. . . {0}. Yeah, that's right. (Press Enter to advance)".format(name)
    input('Hm, I have no idea how I got here.')
    input('Last thing I remember was going to')
    input("sleep, and now I'm here. . .")
    input('*tries to open the door*')
    input("What the. . .!? it's locked!")
    input('Anybody out there? Help!')
    input('. . .')
    input("Damn, guess I'll have to break out of here. . .\n")

def main_hub():
    print('What to do. . .')
    print("(Input 'left' to look left,")
    print("'right' to look right,")
    print("'back' to look behind you,")
    look = input("or 'door' to go to the door.)\n")
    if not look:
    elif look == 'left' or look == 'Left':
        room_sides(mallet, bookshelf, bookshelf_q, crowbar, boarded_hole, pry, mallet_get)
    elif look == 'right' or look == 'Right':
        room_sides(crowbar, box_see, box_q, hand, box_open, box_easy, crowbar_get)
    elif look == 'back' or look == 'Back':
        room_sides(key, crack, crack_q, mallet, crack_complementing, crack_smash, key_get )
    elif look == 'door' or look == 'Door':

def inventory():
    if crowbar == 0:
        print('Inventory: empty')
    elif crowbar == 1 and mallet == 0:
        print('Inventory: crowbar')
    elif crowbar == 1 and mallet == 1 and key == 0:
        print('Inventory: crowbar, mallet')
        print('Inventory: crowbar, mallet, key')

def room_sides(item_get, string1, string2, item_use, string3, string4, string5):
    global crowbar
    global mallet
    global key
    if item_get == 1:
        return main_hub()
    action = input('(1 for yes, 2 for no)\n')
    if not action:
    if action == '1' and item_use == 0:
    elif action == '2':
    elif action == '1' and item_use == 1:
        if item_get == crowbar:
            crowbar =+ 1
            return main_hub()
        elif item_get == mallet:
            mallet =+ 1
            return main_hub()
        elif item_get == key:
            key =+ 1
            return main_hub()

def game_end():
    global key
    if key == 1:
        input("\nHope this works. . .")
        input('*key fits and turns perfectly, unlocking the door*')
        input("Awesome, I'm free!")
        input('Thanks for playing!')
        input("Can't do much with the door for now.\n")


If it helps to know, I attempt to put in at least a few hours a day of practice, and I'm not interested in working in the video game industry, so this would remain as a hobby over anything. Any input is appreciated!


1 Answer 1


The most obvious thing you are missing is any use of Python's various container types, relying instead on too many individual variables. Two examples spring immediately to mind, using the most common containers: list and dict.


At the moment, you are using global variables for your inventory, which is a bad sign. The function for displaying the inventory:

def inventory():
    if crowbar == 0:
        print('Inventory: empty')
    elif crowbar == 1 and mallet == 0:
        print('Inventory: crowbar')
    elif crowbar == 1 and mallet == 1 and key == 0:
        print('Inventory: crowbar, mallet')
        print('Inventory: crowbar, mallet, key')

is also somewhat awkward. Contrast this with:

def display_inventory(inventory):
    print("Inventory: {0}".format(", ".join(inventory) if inventory else "empty"))

How have I made it so neat? By making inventory a list of the items your character has, e.g.:

>>> display_inventory([])
Inventory: empty
>>> display_inventory(['mallet', 'crowbar'])
Inventory: mallet, crowbar

Checking whether the user has the item needed is now the relatively clear:

if 'hammer' in inventory:

as opposed to:

if item_use == 1:

This has also removed reliance on the order in which things are collected, so if you later decide to extend your game it will be much easier.


Second, you have the function room_sides which gets passed lots of arguments, each corresponding to the same variables for different rooms. Compare to:

def perform_action():
    choices = {1: True, 2: False}
    while True:
            choice = int(input('(1 for yes, 2 for no)\n'))
        except ValueError:
            print("Not valid input")
            if choice in choices:
                return choices[choice]
            print("Not valid input")

def room_sides(room, inventory):
    if room['completed']:
        input('\nNothing of interest here.\n')
        if perform_action():
            if room['item needed'] is None or room['item needed'] in inventory:
                for key in ['process', 'outcome', 'item found']:
                room['completed'] = True
                print("That won't work.\n")
            input("\nI'll leave it alone for now.\n")

Again, how have I achieved this? Firstly, using the inventory already defined, rather than the global variables. Secondly, by moving the logic required to take user input out to a separate function (you could so something similar in main_hub, too). Finally, by making each room_side a dictionary, rather than a series of variables:

side1 = {'intro': '\nI see a box.',
         'question': 'I think I can open this. Should I?',
         'item needed': None,
         'process': '\n*box opening sounds*',
         'outcome': 'I opened the box with ease.',
         'item found': 'A crowbar? This will help.\n',
         'item': 'crowbar',
         'completed': False}

Again, note that the reliance on the collection order has been factored out - we don't care about the order items are collected in, just whether the player has what is needed right now.

  • \$\begingroup\$ I would probably go one step further and make the room a class instance rather than a simple dictionary. If you returned the next place to go in each function you could have a loop which asked for input, handled the choice, and went to the next location. \$\endgroup\$
    – Sean Perry
    May 12, 2014 at 23:19
  • \$\begingroup\$ @SeanPerry I considered a class, but it was a big jump from where the OP was; I thought containers was a logical next step. \$\endgroup\$
    – jonrsharpe
    May 12, 2014 at 23:24
  • \$\begingroup\$ I totally understand @jonrsharpe. I just wanted to mention that the path kept going without making a "me too plus more" answer. \$\endgroup\$
    – Sean Perry
    May 12, 2014 at 23:35
  • \$\begingroup\$ @SeanPerry ah, I see; and certainly agree! Thanks for the input. \$\endgroup\$
    – jonrsharpe
    May 12, 2014 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.