0

I create a list and try to append it to another list, but even though it is a global list it still is not defined.

I had the same issue trying to apppend a string to another list, and that had the same error so I tried to make the string a list.

sees if the player hits

def hit_or_stand():
    global hit **<-- notice hit is a global variable**
    if hitStand == ("hit"):
            player_hand()
            card = deck()
            hit = []
            hit.append(card)

now I need to append hit to pHand (player's hand)

def player_hand():
    global pHand
    deck()

    pHand = []

    pHand.append(card)
    deck()
    pHand.append(card)
    pHand.append(hit) **<--- "NameError: name 'hit' is not defined"**
    pHand = (" and ").join(pHand)

    return (pHand)

hit_or_stand()
player_hand()

2 Answers2

2
global hit

This does not declare a variable which is global. It does not create a variable which does not exist. It simply says "if you see this name in this scope, assume it's global". To "declare" a global variable, you need to give it a value.

# At the top-level
hit = "Whatever"
# Or in a function
global hit
hit = "Whatever"

The only time you need a global declaration is if you want to assign to a global variable inside a function, as the name could be interpreted as local otherwise. For more on globals, see this question.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
0

There is a misunderstanding of the global operation in OP's post. The global inside a function tells python to use that global variable name within that scope. It doesn't make a variable into a global variable by itself.

# this is already a global variable because it's on the top level
g = ''

# in this function, the global variable g is used
def example1():
    global g
    g = 'this is a global variable'

# in this function, a local variable g is used within the function scope
def example2():
    g = 'this is a local variable'

# confirm the logic
example1()
print( g ) # prints "this is a global variable"
example2()
print( g ) # still prints "this is a global variable"
Jason K Lai
  • 1,500
  • 5
  • 15
  • Where is the misunderstanding? – melpomene May 19 '19 at 15:15
  • I basically just meant that OP was misusing `global`, thus not understanding its function. – Jason K Lai May 19 '19 at 15:19
  • How is OP misusing `global`? – melpomene May 19 '19 at 15:31
  • OP ran `global hit` expecting that line to make variable `hit` into a global variable (as indicated in the following function/comments where OP tries to call `hit` as if it were global). Instead, when `global hit` was used, it is basically saying that variable `hit` should be a global variable only within that scope (not outside as intended). That's why the post indicated a "NameError: name 'hit' is not defined" error when running that code. – Jason K Lai May 19 '19 at 17:52
  • No, you only need `global` in functions that assign to `hit`. `player_hand` does not assign to `hit`, so it can use it as a global variable directly. See also Silvio Mayolo's answer. – melpomene May 19 '19 at 18:28