0

我有以下代码:

#AON = Amount of Numbers to average
def general():
print "Enter how many numbers you will enter."
print "Maximum amount is 10: "
aon = raw_input()
try:
    aon = int(aon)
    if aon >= 10:
        print "I cannot average more than 10 numbers."
        general()
    else:
        start_average()
except ValueError:
    print "You entered an invalid input, try again."
    general()

def start_average():
    if aon == 1:
        print "You cannot average one number."
        general()
    elif aon == 2:
        def first_number():
            print "First number: "
            first_ni = raw_input()
            second_number()
        first_number()
        def second_number():
            print "Second number: "
            second_ni = raw_input()
            ans_two = first_ni / second_ni
        second_number()
        final_two()
    elif aon == 3:
        def third_number():
            first_number()
            second_number()
            print "Third number: "
            third_ni = raw_input()
            ans_three = ans_two / third_ni
        third_number()
        final_three()
    elif aon == 4:
        def fourth_number():
            first_number()
            second_number()
            third_number()
            print "Fourth number: "
            fourth_ni = raw_input()
            ans_four = ans_three / fourth_ni
        fourth_number()
        final_four()
    elif aon == 5:
        def fifth_number():
            first_number()
            second_number()
            third_number()
            fourth_number()
            print "Fifth number: "
            fifth_ni = raw_input()
            ans_five = ans_four / fifth_ni
        fifth_number()
        final_five


    def final_two():
        final_input = ans_two
        final_answer()
    def final_three():
        final_input = ans_three
        final_answer()
    def final_four():
        final_input = ans_four
        final_answer
    def final_five():
        final_input = ans_five
        final_answer()
    def final_six():
        final_input = ans_six
        final_answer()
    def final_seven():
        final_input = ans_seven
        final_answer()
    def final_eight():
        final_input = ans_eight
        final_answer()  
    def final_nine():
        final_input = ans_nine
        final_answer()  

    def final_answer():
        listofnumbers = [first_ni, second_ni, third_ni, fourth_ni, fifth_ni, sixth_ni, seventh_ni, eight_ni, ninth_ni]
        print "The average of your numbers:"
        print listofnumbers

        print "Is = %d." % final_input

general()

它的目的是找到一个数字的平均值,但是当我通过 PowerShell 运行它时,我收到以下错误:

Traceback (most recent call last):
File "average.py", line 97, in <module>
general()
File "average.py", line 10, in general
general()
File "average.py", line 10, in general
general()
File "average.py", line 12, in general
start_average()
UnboundLocalError: local variable 'start_average' referenced before assignment

我可能在我的代码中做了更多的事情,我只是做了这个,但我只是不知道如何修复它或者显示的错误是什么!我不明白。

4

2 回答 2

2

很难从你粘贴的内容中分辨出来,因为你已经清楚地破坏了缩进。

但它看起来像这样的代码:

print "Enter how many numbers you will enter."
print "Maximum amount is 10: "

… 是 inside general,而这段代码:

aon = raw_input()
try:
    aon = int(aon)
    if aon >= 10:
        print "I cannot average more than 10 numbers."
        general()
    else:
        start_average()
except ValueError:
    print "You entered an invalid input, try again."
    general()

… 意味着处于模块级别。

代码按其出现的顺序执行。函数定义只是代码,就像其他任何东西一样。所以,你不能在定义之前调用一个函数,因为这个函数还不存在。


您可能会反对,举个这样的例子:

def foo():
    bar()

def bar():
    print('Hi!')

foo()

看起来我们bar在它存在之前就调用了它,但它确实有效。如何?

好吧, foo 的定义是在存在之前执行bar的,但这很好。这只是定义了一个函数,它将在运行时调用任何bar方法。只要我们bar在调用它之前已经定义——而且我们已经定义了——一切都很好。


但是,您的代码中有许多类似的问题。例如,让我们看一下这部分:

elif aon == 2:
    def first_number():
        print "First number: "
        first_ni = raw_input()
        second_number()
    first_number()
    def second_number():
        print "Second number: "
        second_ni = raw_input()
        ans_two = first_ni / second_ni
    second_number()
    final_two()

first_nifirst_number函数内的局部变量。每次调用时first_number,都会定义一个新first_ni的,但仅在该函数中可见。你不能在 中使用它second_number,因为second_number只能看到它自己的局部变量和全局变量,first_ni两者都不是。


您需要了解的概念称为范围。Python 有一些很好的工具可以帮助你理解范围。您可以在程序的任何部分打印locals()并查看其中的内容。但是您需要先阅读教程。globals()dir()

于 2013-07-03T23:29:36.807 回答
1

缩进和其他一些事情是有问题的。我不确定你为什么要在其他函数中定义所有这些函数......我认为这会做你想要的。不过,您将需要更多的错误检查(确保输入是整数,除以 0 等)

>>> def getMean(maxNumsToDivide):
...     listNums = []
...     for i in range(maxNumsToDivide):
...         num = raw_input("Please enter a number: ")
...         if not num:
...             break
...         listNums.append(int(num))
...     return float(sum(listNums))/len(listNums)
... 
>>> getMean(100)
Please enter a number: 2
Please enter a number: 3
Please enter a number: 4
Please enter a number: 5
Please enter a number: 3
Please enter a number: 
3.4
于 2013-07-03T23:34:32.607 回答