在赋值Python之前引用

时间:2013-07-03 23:16:30

标签: python python-2.7 average

我有以下代码:

#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

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

2 个答案:

答案 0 :(得分:2)

很难说你粘贴了什么,因为你已经明显打破了缩进。

但看起来像这段代码:

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

...意味着在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(),以查看其中的范围。但是你需要先阅读教程。

答案 1 :(得分: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