TypeError: 'int' 对象在使用 (int()) 函数时在 python 中不是可调用的错误



我的代码中有一个问题,我所理解的是我已经定义了"int";比如int = ("random thing")

我的完整代码如下,如果你需要更多信息

import sys

info = False
digit1 = 0
digit2 = 0
digit3 = 0
digit4 = 0
digit5 = 0
digit6 = 0
digit7 = 0
digit8 = 0
info = (bool(input("do you want extra info? (If not leave blank if yes enter anything)")))
input = (int(input("number from 0-255")))
if (int(input)) > (255):
sys.exit("Wrong")

if input >= 128:

input = input - 128
if info == (True):
print ("above 128")
print("New number:")
print(input)
digit1 = 1
if input >= 64:
input = input - 64
if info == (True):
print ("above 64")
print("New number:")
print(input)
digit2 = 1
if input >= 32:
input = input - 32
if info == (True):
print ("above 32")
print("New number:")
print(input)
digit3 = 1
if input >= 16:
input = input - 16
if info == (True):
print ("above 16")
print("New number:")
print(input)
digit4 = 1
if input >= 8:
input = input - 8
if info == (True):
print ("above 8")
print("New number:")
print(input)
digit5 = 1
if input >= 4:
input = input - 4
if info == (True):
print ("above 4")
print("New number:")
print(input)
digit6 = 1
if input >= 2:
input = input - 2
if info == (True):
print ("above 2")
print("New number:")
print(input)
digit7 = 1
if input >= 1:
input = input - 1
if info == (True):
print ("above 1")
print("New number:")
print(input)
digit8 = 1
print("Your Input In binary is:")
output = (str(digit1) + str(digit2) + str(digit3) + str(digit4) + str(digit5) + str(digit6) + str(digit7) + str(digit8))
print (output)

digit01 = 0
digit02 = 0
digit03 = 0
digit04 = 0
digit05 = 0
digit06 = 0
digit07 = 0
digit08 = 0

input2 = (int(input("number from 0-255")))
if (int(input2)) > (255):
sys.exit("Wrong")

if input2 >= 128:

input2 = input2 - 128
if info == (True):
print ("above 128")
print("New number:")
print(input2)
digit01 = 1
if input2 >= 64:
input2 = input2 - 64
if info == (True):
print ("above 64")
print("New number:")
print(input2)
digit02 = 1
if input2 >= 32:
input2 = input2 - 32
if info == (True):
print ("above 32")
print("New number:")
print(input2)
digit03 = 1
if input2 >= 16:
input2 = input2 - 16
if info == (True):
print ("above 16")
print("New number:")
print(input2)
digit04 = 1
if input2 >= 8:
input2 = input2 - 8
if info == (True):
print ("above 8")
print("New number:")
print(input2)
digit05 = 1
if input2 >= 4:
input2 = input2 - 4
if info == (True):
print ("above 4")
print("New number:")
print(input2)
digit06 = 1
if input2 >= 2:
input2 = input2 - 2
if info == (True):
print ("above 2")
print("New number:")
print(input2)
digit07 = 1
if input2 >= 1:
input2 = input2 - 1
if info == (True):
print ("above 1")
print("New number:")
print(input2)
digit08 = 1
print("Your Input In binary is:")
output2 = (str(digit01) + str(digit02) + str(digit03) + str(digit04) + str(digit05) + str(digit06) + str(digit07) + str(digit08))
print (output2)

a = output
b = output2
max_len = max(len(a), len(b))
a = a.zfill(max_len)
b = b.zfill(max_len)
result = ''
carry = 0
for i in range(max_len - 1, -1, -1):
r = carry
r += 1 if a[i] == '1' else 0
r += 1 if b[i] == '1' else 0
result = ('1' if r % 2 == 1 else '0') + result
carry = 0 if r < 2 else 1
if carry != 0:
result = '1' + result
print(result.zfill(max_len))

是的,我知道有更好的方法来做我正在做的代码,但我的输出是这个

do you want extra info? (If not leave blank if yes enter anything)
number from 0-255 *1*
Your Input In binary is:
Traceback (most recent call last):
File "C:Users10bamDesktopcalculador.ideacalc.py", line 105, in <module>
input2 = (int(input("number from 0-255")))
TypeError: 'int' object is not callable
00000001
Process finished with exit code 1

我知道我已经从我读到的内容中绑定了一些int,但我已经检查过了,不是这样的

除了input之外,您不将int绑定到其他任何东西。您将input绑定到第16行中的一个整数:

input = (int(input("number from 0-255")))

然后在第105行尝试使用input作为函数调用:

input2 = (int(input("number from 0-255")))

此时,全局名称空间字典包含键值对'input': 0。Python不会在builtins中搜索input函数,而是直接从全局名称空间字典中获取input的值并调用它,因此会得到错误。

为了解决这个问题,请将变量重命名为input,并记住不要让您的变量与内置函数同名。

最新更新