我已经使用Python进行了这个小型编码练习已经几个小时了:
def collatz(number):
if number % 2 == 0:
print(number // 2)
return number // 2
elif number % 2 == 1:
result = 3 * number + 1
print(result)
return result
print('Enter a number: ')
try:
enter = int(input())
except ValueError:
print('Please type an integer. Try again...')
while enter != 1:
enter = collatz(enter)
[输入数字似乎很正常,当我输入一个非整数值时,我得到了正确的响应,但在响应后仍然出现此错误:
Enter a number:
Hello
Please type an integer. Try again...
Traceback (most recent call last):
File "C:/Users/P1ttstop/PycharmProjects/sweigartLearning/collatzSequence.py", line 17, in <module>
while enter != 1:
NameError: name 'enter' is not defined
我一直在尝试解决此问题,但令我沮丧的是,我似乎无法弄清楚。这是怎么回事?
我已经测试了代码,并修改了一部分。
来自Python文档,一个NameError异常是: