我刚刚开始学习 python 并不断收到一个我无法弄清楚的错误。任何帮助将不胜感激。基本上,我不断收到以下错误:
Enter an int: 8
Traceback (most recent call last):
File "C:\Users\Samuel\Documents\Python Stuff\Find Prime Factors of Number.py", line 16, in <module>
once_cycle()
File "C:\Users\Samuel\Documents\Python Stuff\Find Prime Factors of Number.py", line 8, in once_cycle
while x==0:
UnboundLocalError: local variable 'x' referenced before assignment
我看到很多人都有同样的问题,但是当我看到人们告诉他们做什么时,我无法弄清楚。无论如何,我的代码是这样的。我重新检查了所有的缩进,看不出有什么问题。这个程序的目的是找到一个 int 的主要因素(尽管它只完成了 90%)。它是用 Python 2.7.3 编写的。
import math
testedInt = float(raw_input("Enter an int: "))
workingInt = testedInt
x = 0
def once_cycle():
for dividor in range(1, int(math.floor(math.sqrt(testedInt))+1)):
while x==0:
print "Called"
if (workingInt%dividor == 0):
workingInt = workingInt/dividor
x = 1
if (workingInt > 1):
once_cycle()
return
once_cycle()
print workingInt
提前感谢您的帮助,
山姆