如何从Python中的另一个函数调用int()函数?
以下是我的代码:如何从Python中的另一个函数调用int()函数?,python,Python,以下是我的代码: class MyClass: "This is my second class" a=["1224","2334","3445"] b=int(input("Enter the PIN number")) entries={1224:"X",2334:"Y",3445:"Z"} def check(self,int): che=False
class MyClass:
"This is my second class"
a=["1224","2334","3445"]
b=int(input("Enter the PIN number"))
entries={1224:"X",2334:"Y",3445:"Z"}
def check(self,int):
che=False
for c in range(0,3):
if (m.b==int(m.a[c])):
che=True
break
else:
che=False
print(che)
def func(int):
print('Hello',entries[b])
m=MyClass()
m.check(m.b)
每当我尝试从方法内部将字符串转换为整数时,我都会得到TypeError:
Enter the PIN number1224
Traceback (most recent call last):
File "MyClass.py", line 18, in <module>
m.check(m.b)
File "MyClass.py", line 9, in check
if (m.b==int(m.a[c])):
TypeError: 'int' object is not callable
两件事
我无法准确地再现您的错误,但是我没有得到正确的输出,有关如何获得正确的输出,请参见第2部分
为什么要将输入转换为整数,因为当前列表中有字符串,所以要将输入保留为字符串。
通常在您的check方法中,将其命名为self.a和self.b,因为这意味着该类可以被命名为任何名称,而当前,如果您的类未命名为m,则可能会导致问题
编辑:同样在for循环中,在第一个if语句中,在将che设置为true后添加一个中断,否则它将继续运行for循环,并可能在将che设置为true后将其设置为false请添加完全回溯。一个建议。在您的check方法中,执行'self.b'和self.a,而不是m.b和m.a,因为这将允许类的任何名称