如果错误消息表明在赋值之前引用了变量,这意味着什么?

时间:2015-02-06 10:06:52

标签: python python-3.x

def Q7():
    names=['ben','thor','zoe','kate'] #the list of names 
    Max = 4
    found=False
    Splayer = input("What plaer are you looking for? ")
    while found == False and current <= Max:
        if names[current]== Splayer:
            found = true
        else:
            current = current+1
        if found == True:
            print('yes, they have a top')
        else:
            print('no,they do not have a top score')

这是一个程序要求我执行以下操作:找到播放器名称并根据用户输入打印出一个输出但是当我运行它时找到= = False和当前&lt; = Max: UnboundLocalError:局部变量&#39; current&#39;在分配之前引用 我如何让它正常工作

3 个答案:

答案 0 :(得分:2)

def Q7():
    names=['ben','thor','zoe','kate'] #the list of names 
    Max = 4
    found=False
    Splayer = input("What plaer are you looking for? ")
    current = 0;
    while found == False and current < Max:
        if names[current]== Splayer:
            found = True
        current = current+1
    if found == True:
        print('yes, they have a top')
    else:
        print('no,they do not have a top score')
  1. 找到的检查在您完成搜索后完成,而不是在
  2. 内完成
  3. current声明为0。
  4. 从当前循环 - &gt; 0到Max-1
  5. 发现True不是true

答案 1 :(得分:0)

def Q7():
    names=['ben','thor','zoe','kate'] #the list of names 
    Max = 4
    found=False
    current = 0
    Splayer = input("What plaer are you looking for? ")
    while found == False and current <= Max-1:
        if names[current]== Splayer:
            found = True
        else:
            current = current+1
        if found == True:
            print('yes, they have a top')
        else:
            print('no,they do not have a top score')

答案 2 :(得分:0)

tobias_k是对的。你真正想要的是:

names = ['ben', 'thor', 'zoe', 'kate']
player = input("What player are you looking for? ")
if player in names:
    print("Yes, they have a top score.")
else:
    print("No, they don't.")