【发布时间】:2020-11-23 19:18:12
【问题描述】:
global max_n
max_n = 0
def dfs(_n, cnt, length):
if cnt == length:
print(a)
total = 0
for i in range(cnt):
total += int(a[i]) * (10 ** i)
if total % 30 == 0:
if total > max_n:
max_n = total
如果我使用此代码,则if total > max_n: 中存在错误。
即使全局变量max_n在第1行,为什么会出现这个错误?
你能教我如何调试这个错误案例吗?
【问题讨论】:
-
你在哪里初始化
a?_n, cnt, length使用了哪些值? -
您的代码返回
NameError: name 'a' is not defined。如果你在其他地方定义了a,你应该在你的代码中显示它,并提供这个函数是如何被调用的。
标签: python global-variables local-variables