简体   繁体   English

初学者试图调试一个简单的程序

[英]Beginner trying to debug an easy program

I'm a student and am just beginning to learn code. 我是学生,刚开始学习代码。 Right now I'm working with Python and have a program I think should work, but just returns some errors that I don't understand: 现在我正在使用Python并且有一个我认为应该工作的程序,但只返回一些我不理解的错误:

Traceback (most recent call last): File "C:\\Program Files\\Notepad++\\1913lab3.py", line 23, in print(most(odd, [])) 回溯(最近一次调用最后一次):文件“C:\\ Program Files \\ Notepad ++ \\ 1913lab3.py”,第23行,打印(大多数(奇数,[]))

File "C:\\Program Files\\Notepad++\\1913lab3.py", line 9, in most N = S[i] UnboundLocalError: local variable 'i' referenced before assignment 文件“C:\\ Program Files \\ Notepad ++ \\ 1913lab3.py”,第9行,大多数N = S [i] UnboundLocalError:在赋值之前引用的局部变量'i'

I don't understand what the first error tells me, but the I think I understand the second one, but I don't understand why I'm getting it. 我不明白第一个错误告诉我的是什么,但我想我理解第二个错误,但我不明白为什么我会得到它。 I don't think i is a local variable as I defined it right away in the beginning. 我不认为我是一个局部变量,因为我在开始时立即定义它。 Here's the code: 这是代码:

t = 0
f = 0
i = 0

def odd(N):
    return N % 2 != 0

def most(P, S):
    N = S[i]
    if P == True:
        t += 1
    else:
        f += 1
    i += 1
    if i < len(S):
        most(P, S)
    else:
        if t > f:
            return 'True'
        else:
            return 'False'

print(most(odd, []))
print(most(odd, [0]))
print(most(odd, [1]))
print(most(odd, [1, 2]))
print(most(odd, [1, 2, 3]))

The assignment is to define a recursive function (most()). 赋值是定义递归函数(most())。 The function takes one function and one list as its arguments (P and S). 该函数将一个函数和一个列表作为其参数(P和S)。 I can't use loops or local variables. 我不能使用循环或局部变量。 Here's a quote from the assignment: 这是作业的引用:

"P is a function of one argument that returns either True or False, and S is a list. The function most calls P on each element of S. It must return True if P returns True more often than it returns False. It must return False otherwise." “P是一个参数的函数,返回True或False,S是一个列表。函数最多在S的每个元素上调用P.如果P返回True的次数多于返回False,则必须返回True。它必须返回否则就错了。“

The 5 print commands are just 5 examples that I need to work for credit, but this program needs to work for all lists. 5个打印命令只是我需要为信用工作的5个例子,但是这个程序需要适用于所有列表。 If anyone can help me fix these errors, that'd be great. 如果有人可以帮助我解决这些错误,那就太好了。 (Also, any general Python tips would be welcome.) (此外,欢迎任何一般的Python技巧。)

By default any variable that is modified within a function is assumed to be local to that function. 默认情况下,假定在函数内修改的任何变量都是该函数的本地变量。 So if you have i += 1 , i must be defined within the function first. 所以如果你有i += 1i必须先在函数中定义。 Or you have to declare i as a global first ( global i ) so that python knows it is refering to the i you have defined (first) outside the function. 或者你必须声明i作为全球第一( global i )这样Python知道它是指的对i您已经定义(第一)之外的功能。 But beware with globals, they are often dangerous (as they make it hard to keep track what is going on) and should be avoided if possible. 但要小心全局,它们通常很危险(因为它们很难跟踪发生的事情),如果可能的话应该避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM