Applesausce Applesausce - 2 months ago 6
Python Question

UnboundLocalError: Says variable isn't defined before being used

when i run my game function i get this error:

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "hog.py", line 162, in play
if is_swap(score0,score1)== True and who == 0:
File "hog.py", line 121, in is_swap
elif swap1 == score0:
UnboundLocalError: local variable 'swap1' referenced before assignment


Here are the two functions referenced in the error(first the parent, then child function):

def play(strategy0, strategy1, score0=0, score1=0, goal=GOAL_SCORE):
"""Simulate a game and return the final scores of both players, with
Player 0's score first, and Player 1's score second.

A strategy is a function that takes two total scores as arguments
(the current player's score, and the opponent's score), and returns a
number of dice that the current player will roll this turn.

strategy0: The strategy function for Player 0, who plays first
strategy1: The strategy function for Player 1, who plays second
score0 : The starting score for Player 0
score1 : The starting score for Player 1
"""
who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
# BEGIN Question 5
while score0 <= goal and score1 <= goal:

#Palyerx Turn
if who == 0:
score0 += take_turn(strategy0(score0,score1), score1, select_dice(score0,score1))
elif who == 1:
score1 += take_turn(strategy1(score1,score0), score0, select_dice(score1,score0))
print(score0,score1)
#Swine Swap
if is_swap(score0,score1)== True and who == 0:
temp = score0
score0 = score1
score1 = temp
elif is_swap(score1,score0) == True and who == 1:
temp = score1
score1 = score0
score0 = temp

who = other(who)
# END Question 5
return score0, score1



def is_swap(score0, score1):
"""Return True if ending a turn with SCORE0 and SCORE1 will result in a
swap.

Swaps occur when the last two digits of the first score are the reverse
of the last two digits of the second score.
"""
# BEGIN Question 4
#If scores are equal there is no need to flip
if score0 == score1:
return True
#Flipping Score0
if score0 >= 10 and score0 < 100:
s0String = str(score0)
x = s0String[0]
y = s0String[1]
swap0 = int(y+x)
elif score0 < 10:
x = '0'
y = str(score0)
swap0 = int(y+x)
elif score0 > 100:
s0String = str(score0)
x = s0String[0]
y = s0String[1]
z = s0String[2]
swap0 = int(z+y)
#Flipping Score1
if score1 >= 10 and score1 < 100:
s1String = str(score1)
i = s1String[0]
j = s1String[1]
swap1 = int(j+i)
elif score1 < 10:
i = '0'
j = str(score1)
swap1 = int(j+i)
elif score1 > 100:
s1String = str(score1)
i = s1String[0]
j = s1String[1]
f = s1String[2]
swap1 = int(f+j)
#Swapping Scores Bases on Flipped equivelence
if swap0 == score1:
return True
elif swap1 == score0:
return True
else:
return False


I'm not sure what it wants. As it stands swap0 is defiend before it is used in the is swap function, and it is being called correctly in play.

Answer

None of the branches of your if/elif logic handle the situation where score1 is exactly 100. This leaves swap1 undefined, leading to the error you've described.

To avoid this, make sure your different conditions cover all branches. Often this is easiest if you leave the last branch a regular else rather than an elif. You can also simplify your conditions by changing the order you check them in, like this:

if score1 < 10:
    i = '0'
    j = str(score1)
    swap1 = int(j+i) 
elif score1 < 100:
    s1String = str(score1)
    i = s1String[0]
    j = s1String[1]
    swap1 = int(j+i) 
else: # score1 >= 100
    s1String = str(score1)
    i = s1String[0]
    j = s1String[1]
    f = s1String[2]
    swap1 = int(f+j)   
Comments