小编Zaf*_*aji的帖子

UnboundLocalError:赋值前引用的局部变量“len”

print "welcome to the average finder"
print """Insert the values you want to find the average for,
when you insert all the values type 'done'"""

values = []

def insert_values():
    action = raw_input("> ")

    if 'done' in action :
        calculating_average()
    elif action != 'done':
        x = int(action)
        values.append(x)
        insert_values()
    else:
        print "please insert values then type done"
        insert_values()

def calculating_average():

    len = len(values)
    sum = sum(values)

    final = sum / len 
    print final

insert_values()
Run Code Online (Sandbox Code Playgroud)

该脚本的目的:向用户询问数字,然后让脚本找到数字的平均值并显示它。我创建了一个名为值的空列表,将用户输入的数字转换为整数后附加它们。

我不断收到 UnboundLocalError: 在赋值之前引用局部变量 'len'...即使我认为我确实将 len …

python python-2.7

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

python-2.7 ×1