UnboundLocalError:表示在使用之前未定义变量

时间:2016-09-15 00:41:48

标签: python

当我运行我的游戏功能时,我收到此错误:

    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

以下是错误中引用的两个函数(首先是父函数,然后是子函数):

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

我不确定它想要什么。因为它代表swap0在它被用于交换函数之前是defiend,并且它在游戏中被正确调用。

1 个答案:

答案 0 :(得分:1)

if / elif逻辑的所有分支都没有处理score1正好为100的情况。这会导致swap1未定义,从而导致出错#39已经描述过了。

为避免这种情况,请确保您的不同条件涵盖所有分支。如果您将最后一个分支留给常规else而不是elif,这通常是最简单的。您还可以通过更改签入的顺序来简化条件,如下所示:

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)