'int' 对象没有属性 'output'
'int' object has no attribute 'output'
这可能是个愚蠢的问题。我刚刚创建了一个函数,它除以一个数字并在数字为偶数时给出余数,否则在 python.
中进行一些其他数学运算
def collataz(number):
if number % 2 == 0:
output = number // 2
return int(output)
else:
output = 3 * number + 1
return int(output)
import sys
try:
c = collataz(int(input("Enter int")))
while c.output != 1:
print(c)
except KeyboardInterrupt:
sys.exit()
但是当我 运行 这段代码在请求输入后出现以下错误 'int' object has no attribute 'output'
我该如何解决这个问题?并请告诉我我的代码出现此错误的原因,以便我理解问题。有什么不明白的地方请指出。
谢谢,
collatz
函数中的名称 output
在外部没有任何意义。您 return
的值只是一个整数,您将其分配给名称 c
.
我认为您希望代码的最后一部分类似于:
c = int(input("Enter int"))
while c != 1:
c = collataz(c)
print(c)
这可能是个愚蠢的问题。我刚刚创建了一个函数,它除以一个数字并在数字为偶数时给出余数,否则在 python.
中进行一些其他数学运算def collataz(number):
if number % 2 == 0:
output = number // 2
return int(output)
else:
output = 3 * number + 1
return int(output)
import sys
try:
c = collataz(int(input("Enter int")))
while c.output != 1:
print(c)
except KeyboardInterrupt:
sys.exit()
但是当我 运行 这段代码在请求输入后出现以下错误 'int' object has no attribute 'output'
我该如何解决这个问题?并请告诉我我的代码出现此错误的原因,以便我理解问题。有什么不明白的地方请指出。
谢谢,
collatz
函数中的名称 output
在外部没有任何意义。您 return
的值只是一个整数,您将其分配给名称 c
.
我认为您希望代码的最后一部分类似于:
c = int(input("Enter int"))
while c != 1:
c = collataz(c)
print(c)