Python UnboundLocalError:局部变量';n';分配前参考

Python UnboundLocalError:局部变量';n';分配前参考,python,dice,Python,Dice,在代码底部添加if语句后,出现此错误: UnboundLocalError:赋值前引用了局部变量“n” 你知道怎么了吗 import random n=input('Write number of spaces you want to be playing on: ') if n < 3: print "Gotta be more than 3 mate" a = input('Would you like to end the program and try a

在代码底部添加
if
语句后,出现此错误:

UnboundLocalError:赋值前引用了局部变量“n”

你知道怎么了吗

import random

n=input('Write number of spaces you want to be playing on: ')
if n < 3:    
    print "Gotta be more than 3 mate"
    a = input('Would you like to end the program and try again?')
    exit()
else:    
    print"You're playing on %d lines" % n

def DieGame(dieSize,numberDie):   # dieSize (6 sides = 6),number of die (in this case,it'll be one
    result = 0
    value = 0
    rounds = 1

    while n > 1:
        print "Round number " ,rounds

        for i in range(0,numberDie):         # starting at 0 , number of die
            result = random.randint(1,dieSize)  #random number, 1 - size of dice(6 in this case)
            print("On %d dice/die,you've rolled %d." % (i+1,result))  
            value += result

        print("Value of the roll/s %d of the dice/die is: %d" % (numberDie,value))

        if (n - value) > 1:     #if I'll get anything else than 1,its okay
            print "New position: " ,n-value
        if n == 1:
            print "You've reached 1 space right before the finish,return back by 1."
            n += 1                   
        if (n-value) == 0:

            a = input('Congratulations, you made it to the end! Press Enter to close the game.')
            exit()

        rounds += 1
    else:
        print "End"
    DieGame(6,1)
#-------------------------------------------------------
#Gotta break the loop once I get to 0,or If I was to cross 0 to -1 and less.Can't cross 0.

#If I'll be like 4 spaces from finish and I'll roll 6,I wont be able to move,gotta reach the last space.

#Gotta make it reroll if I roll 1,add rolls to sum and then move forward by that number
#if rolled for example 1+1+1(rerolling) + 3,add the sum together and move by 6.   

#if you step on the last space before the finish ,get one space back
随机导入
n=input('写入要播放的空间数:')
如果n<3:
打印“必须超过3个伙伴”
a=输入('是否要结束程序并重试?')
退出()
其他:
打印“您正在%d行上播放”%n
def DieGame(dieSize,numberDie):#dieSize(6边=6),模具数量(在本例中,为一个
结果=0
值=0
轮数=1
当n>1时:
打印“轮数”,轮数
对于范围(0,编号)内的i:#从0开始,模具编号
结果=随机。randint(1,dieSize)#随机数,1-骰子大小(本例中为6)
打印(“在%d个骰子/骰子上,您掷了%d”。%(i+1,结果))
值+=结果
打印(“骰子/骰子的卷/秒%d的值为:%d”%(数字,值))
如果(n-value)>1:#如果我得到的不是1,那没关系
打印“新位置:”,n-值
如果n==1:
打印“您在完成之前已到达1个空格,请在1之前返回。”
n+=1
如果(n值)==0:
a=input('祝贺你,你成功了!按Enter键关闭游戏。'))
退出()
回合数+=1
其他:
打印“结束”
迪加梅(6,1)
#-------------------------------------------------------
#一旦我到了0,就要打破这个循环,或者如果我要从0过到-1或更小。不能从0过。
#如果我离终点还有4格,我将滚6格,我将无法移动,必须到达最后一格。
#如果我掷1,将掷骰数加到和上,然后按那个数字向前移动,必须使它重新掷骰
#如果滚动,例如1+1+1(重新滚动)+3,将总和相加并移动6。
#如果你在终点前踩到最后一个空格,就要再踩回一个空格

您的问题是
n
被分配到函数之外。这将导致问题:

>>> n = 3
>>> def x():
...     while n > 1:
...         print("Iterating. n={}".format(n))
...         n = n - 1
... 
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in x
UnboundLocalError: local variable 'n' referenced before assignment
在特定词法范围内使用的变量需要在此处定义。如果闭包不包括
n
,则尝试引用它会导致问题。

可能重复
>>> def y():
...     n = 3
...     while n > 1:
...         print("Iterating. n={}".format(n))
...         n = n - 1
... 
>>> y()
Iterating. n=3
Iterating. n=2