UnboundLocalError: Says variable isn't defined before being used
<p>when i run my game function i get this error:</p>

<pre><code>    Traceback (most recent call last):
  File "&lt;stdin&gt;", line 2, in &lt;module&gt;
  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
</code></pre>

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

<pre><code>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 &lt;= goal and score1 &lt;= 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 &gt;= 10 and score0 &lt; 100:
        s0String = str(score0)
        x = s0String[0]
        y = s0String[1] 
        swap0 = int(y+x)   
    elif score0 &lt; 10:
        x = '0'
        y = str(score0)
        swap0 = int(y+x) 
    elif score0 &gt; 100:
        s0String = str(score0)
        x = s0String[0]
        y = s0String[1]
        z = s0String[2]
        swap0 = int(z+y)
#Flipping Score1
    if score1 &gt;= 10 and score1 &lt; 100:
        s1String = str(score1)
        i = s1String[0]
        j = s1String[1]
        swap1 = int(j+i) 
    elif score1 &lt; 10:
        i = '0'
        j = str(score1)
        swap1 = int(j+i) 
    elif score1 &gt; 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
</code></pre>

<p>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.</p>

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

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

<pre><code>if score1 &lt; 10:
    i = '0'
    j = str(score1)
    swap1 = int(j+i) 
elif score1 &lt; 100:
    s1String = str(score1)
    i = s1String[0]
    j = s1String[1]
    swap1 = int(j+i) 
else: # score1 &gt;= 100
    s1String = str(score1)
    i = s1String[0]
    j = s1String[1]
    f = s1String[2]
    swap1 = int(f+j)   
</code></pre>

python