0

I am trying to change the value of player_score, cpu_score, and ties which were created outside the function determineWinner but I get an error such as this: UnboundLocalError: local variable 'cpu_score' referenced before assignment. I have deleted the other assignments (player_score=player_score+1 and ties=ties+1) and added the print ("Player Score",player_score) to test the program.

I don't understand is why print ("Player Score",player_score) outputs the value assigned outside of the function but cpu_score=cpu_score+1 yields the "referenced before assignment" error.

Also showRolls does what it is supposed to.

It seems that the functions can read the values of the variables assigned outside of the functions but can not change the values. Why is that?

import random

def showRolls():
    print("Player 1 roll:",player_roll)
    print("CPU roll:",cpu_roll)

def determineWinner():
    if player_roll > cpu_roll:
        print("Player wins!")
        print ("Player Score",player_score)
    elif cpu_roll > player_roll:
        print("CPU wins!")
        cpu_score=cpu_score+1
    else:
        print("It's a tie.")

#-----------------------MAIN PROGRAM------------------------------------
player_score=0
cpu_score=0
ties=0
player_roll = random.randint(1,6)
cpu_roll = random.randint(1,6)
showRolls()
determineWinner()
  • 1
    Please read LEGB scoping rules of python. http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules – James Sapam Sep 23 '14 at 04:44
  • While you _can_ fix this, it's probably a bad idea. You might want to consider wrapping this in a class, or passing arguments into the function and returning values back out. Global variables are bad. – abarnert Sep 23 '14 at 04:49
  • There are a dozen other questions that this is more directly a dup of… but they're all closed as dups of [Python variable scope error](http://stackoverflow.com/questions/370357/python-variable-scope-error), so I guess that's the canonical version. If it's not clear how this is the same, click a couple of the dozens of Linked and Related questions from there; about half of them are identical to this. – abarnert Sep 23 '14 at 04:53

1 Answers1

2

Within your function declare any variables that are external to your function, and that you want to change, as global. Reading a global variable does not require the declaration. There is some explanation here.

So, your function should be:

def determineWinner():
    global cpu_score
    if player_roll > cpu_roll:
        print("Player wins!")
        print ("Player Score",player_score)
    elif cpu_roll > player_roll:
        print("CPU wins!")
        cpu_score=cpu_score+1
    else:
        print("It's a tie.")
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • No !!!! WHy do people keep suggesting using Globals - please - globals are rarely the right solution, and here, they are certainly not. The right solution here is encapsulating the data in a class. Globals are a source of hard to find bugs, and should be actively discouraged. – Tony Suffolk 66 Sep 23 '14 at 07:41
  • @TonySuffolk66 : beg your pardon but, while i quite agree with your anit-global rant, the OP asked why their code doesn't work and that is what has been answered. No one is suggesting nor recommending use of globals, and others had already commented to discourage their use. – mhawke Sep 23 '14 at 10:51
  • Apologies for the tone :-) I just see globals and shudder :-( – Tony Suffolk 66 Sep 23 '14 at 13:37