Exercise 36 - My little game

Hi Fellow Pythonists,

I’ve created a little game per exercise 36 and I’ve had loads of fun.

I’m in the middle of debugging it right now, so s’cuse the bugs please.

I need suggestions of how I can globally view my ‘inventory’. I know that I could write code to every if statement from each def function, but I want to be able to write one piece of code and then call it at anytime without having to write that piece of code all over the place.

So for instance - I’d like it to where when a user is playing the game, they could just type ‘inventory’ and they could check/see the inventory list at any given time. And I was looking to achieve that with just one piece of code I write once but works globally. Is that possible?

I wrote this in python 3.x

I hope you all like the game, I’m doing more things to it by adding more dialog and making the battle scenes more intuitive with more decisions while fighting. I could figure that part out myself.

Thanks and enjoy :slight_smile:

@Pauly Because inventory is a list it is automatically globaly visible. You can access and update it from everywhere also from inside functions. Lists have not the same scope as variables.

@DidierCH thanks for looking at the code.

Totally understand. I’ve tried that before and it works, but I think I failed to explain more.

What I really need (and I"ll update the description), is when someone is playing the game, they could just type ‘inventory’ and they’ll could see the inventory list at any given time. And I was looking to achieve that with just one global piece of code. Is that possible?

@Pauly Okay. I understand. I crafted some possible solution below.

The idea is that you have a function for the choices which you can call with specific arguments for every room.

But be aware that eval is not a good choice if you use user input with it for security reasons. In this case I don’t use user input I just use it to convert a string to python code.

This solution is at that stage of knowlege (ex36) some possible solution. But I know that we will have more choices as soon as we start with OOP on ex43 and beyond.

def inventory():
    print("Your inventory")
    inventory_list= ["sword", "knife"]

def start():
    choice_one = "left"
    choice_two = "right"
    action_one = "gold_room()"
    action_two = "dead()"
    print("You're in start")
    print("Print 'inventory' for inventory")
    print("Print 'left' for Gold Room")
    print("Print 'right' for dead")

    choices(choice_one, choice_two, action_one, action_two)

def gold_room():
    choice_one = "w"
    choice_two = "n"
    action_one = "start()"
    action_two = "dead()"
    print("You're in gold room")
    print("Print 'inventory' for inventory")
    print("Print 'w' for start")
    print("Print 'n' for dead")
    choices(choice_one, choice_two, action_one, action_two)

def dead():
    print("your dead")

def choices(input_1, input_2, action_1, action_2):
    while True:
        choice = input(">>> ")

        if choice == "inventory":
            print("You typed 'inventory'")
        elif input_1 in choice:


You want the global keyword I believe. So, put an INVENTORY variable at the top, set to a list of things or a dict.

Then in every function that you want to work with that you put:


Now, in theory you don’t need the global keyword if you’re using a dict or a list. You can always alter those, you just can’t assign anything to them. What that means is this will work without global:


But, this won’t work:

INVENTORY = [‘sword’]

To be safe and to do some documentation, just always put a global INVENTORY at the top of functions that use a global.

1 Like

Hi @zedshaw, thanks for checking this out.

I’m targeting the check inventory part to work similar to the way Zorg does, where the player can just type “inventory” from any part of the game to view their entire inventory

But I’m trying to avoid having to code check_inventory() function within each if statement. So I was assuming that their was some sort of way to create a function or variable where the developer just codes it once, and the player can just type ‘inventory’ to check their inventory from the console, but from anywhere

I’m already using a list at the top.


Then throughout the game the user finds treasure in which I give the player the option to keep the treasure with input(). Then if they do keep it, I use .append() to add each one to the list.

inventory.append(‘Atlantian sword’)
inventory.append(‘Aegis sheild’)
inventory.append(‘Blacksmith key’)

So, I created a function called check_inventory() that I could use to ask the player if they’d like to review their inventory, but found I have to add that function to every input() function and if statements, then the check_inventory() function gets executed and shows the list.

it works, but I’m looking for something where I don’t have to ask the user if they’d like to check their inventory and add have to add the check_inventory() function everytime.

Honestly, in this game you have to do it this way. At the end of the book you do a game that allows you to parse the input from the user and then do what you need before dealing with room mechanics. That’s the game where you can do this.

1 Like

Awesome!! I’m so happy that there IS a way.

I can’t wait to get to that section. I’m dying to see how its done.

@zedshaw thanks, I appreciate the tips!