0

I am trying to figure out a way to change a global variable from False to True if function rope is called. With my existing code, what could I add to make this possible?

Also, the global variable that exists is called inventoryRope, and it starts off as False.

Here is my code:

def rope():

    print("You pick up the rope.")

    command = input("Type CONTINUE to carry on.")
    if command == "CONTINUE":
        nextScene()
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 1
    https://stackoverflow.com/questions/10588317/python-function-global-variables – dvdktn Oct 15 '18 at 00:27
  • 1
    Avoid global variables; nest the global variable within the scope of another "main" function and perform this logic within that function's scope. – Sean Pianka Oct 15 '18 at 00:29
  • 1
    Why don't you change it from within the function? That would always occur when the function is called. To be frank, though, these design decisions (mutable global state) are classic anti-patterns. Of course, a good way to really learn that for yourself is to write code this way, and see how unmanageable it becomes as your code base grows. It's sort of a rite of passage we all go through. – juanpa.arrivillaga Oct 15 '18 at 00:29
  • @juanpa.arrivillaga Thank you for the advice! I've been told that global variables can be troublesome, but I'm fairly new to python (as this is for a high school coding project) and I'm unsure of other ways to go about what I'm trying to achieve. Hopefully I'll learn with experience! – Brooke E Oct 15 '18 at 00:35
  • It's the best way. You'll learn different patterns as you progress. The two popular programming paradigms right now, object-oriented and functional, are essentially two different approaches to handling state in your program. But for this sort of project, just use what works and start learning. Happy coding! – juanpa.arrivillaga Oct 15 '18 at 00:38
  • It's not helpful to suggest "just use what works" in the context of "start learning;" learning bad habits at the beginning is extremely detrimental. Global variables aren't just troublesome, they make your code harder to read, and they can result in otherwise-idempotent functions losing that property; that is, the same function called with the same arguments may yield different results due to the value of a global variable, which makes it harder to understand what is happening and what can happen anywhere that variable is used. – kungphu Oct 15 '18 at 01:02

2 Answers2

0

Need to use global:

inventoryRope = False

def rope():
    global inventoryRope

    print("You pick up the rope.")

    command = input("Type CONTINUE to carry on.")
    if command == "CONTINUE":
        inventoryRope = True
        nextScene()
user3713719
  • 325
  • 3
  • 8
0

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")
kungphu
  • 4,592
  • 3
  • 28
  • 37