Your actual goal here appears to be tracking inventory and making its state accessible from multiple functions. In that context, your approach will work, but it doesn't scale well to arbitrary numbers of inventory items.
inventory_rope = False
def pick_up(item):
if item == "rope":
inventory_rope = True
def use(item):
if (item == "rope") and inventory_rope:
print("Used rope")
Note: Because it looks like you're keeping this very simple, I'm using strings for items here. There are certainly better ways to do this.
There are many potential ways to handle inventory; one would be to simply create a list to handle whatever the player picks up.
inventory = []
def pick_up(item):
print("You picked up", item)
inventory.append(item)
def use(item):
print("Used", item)
inventory.remove(item)
You could instead inherit from the built-in list
type, if you want to build in additional/different behaviors.
class Inventory(list):
def append(self, item):
print("You picked up", item)
super().append(item)
inventory = Inventory()
inventory.append("rope")
Another possibility would be making the inventory an attribute of your player object, if there are other things the player can do that would make sense to build into a class.
class Player(object):
_inventory = None
def __init__(self):
# Note: Don't use mutable objects as default arguments
self._inventory = []
def pick_up(self, item):
print("You picked up", item)
self._inventory.append(item)
player = Player()
player.pick_up("rope")