Python报错TypeError: 'str' object is not callable,求排查解决
解决
TypeError: 'str' object is not callable 错误 我一眼就看出问题大概率出在你不小心把内置函数input当成了字符串变量来赋值——这是新手常犯的小失误!
结合你的代码片段和错误提示,几乎可以肯定:在代码未贴出的后半部分,你写了类似 input = "某个字符串" 的语句。比如如果你的完整代码里有一行像 input = geld_a 或者其他把input赋值为字符串的操作,那之后再调用input()的时候,Python就会认为input是一个字符串,而不是用来获取用户输入的函数,自然就会抛出'str' object is not callable的错误。
修复步骤:
- 第一步:检查整个代码,找到所有把
input作为变量名的地方,把变量名改成其他的(比如user_input、input_str),绝对不能用input这个内置函数名当变量名。 - 第二步:完善第二个
while循环的结束条件,比如当用户输入合法金额后设置kontostand = True,避免无限循环。
修正后的完整示例代码:
pin = '4372' konto = '5000' pin_s = False kontostand = False # PIN验证循环 while not pin_s: pin_e = input("Bitte geben Sie ihren Pin ein: ") if pin == pin_e: pin_s = True else: print("Falscher PIN, bitte erneut versuchen!") # 增加错误提示更友好 print("Ihr Kontostand beträgt: ", konto) # 取款操作循环(补全逻辑) while not kontostand: geld_a = input("Bitte geben sie den Betrag ein den sie abheben möchten: ") # 验证输入是否为数字且余额足够 if geld_a.isdigit() and int(geld_a) <= int(konto): konto = str(int(konto) - int(geld_a)) print(f"Sie haben {geld_a} Euro abgehoben. Neuer Kontostand: {konto}") kontostand = True else: print("Ungültiger Betrag oder nicht genügend Guthaben! Bitte erneut versuchen.")
错误原因解释:
Python的内置函数(比如input()、print())有固定的功能,不能被当成普通变量名使用。如果你给这些函数名赋值,就会覆盖它们原本的功能——比如当你写input = "hallo",input就从一个函数变成了字符串,之后再调用input()就相当于试图“调用”一个字符串,这显然不符合语法规则,所以会抛出错误。
下次遇到这类错误,先检查自己有没有用内置函数名当变量名,这通常是最快的排查方向!
内容的提问来源于stack exchange,提问作者Kael 90
相关产品推荐
相关产品推荐

