Python运行报TypeError: 'list' object is not callable如何修复
问题原因
你在代码中重写了Python内置的input()函数标识符:
- 第一次循环执行
input = input1.split()时,原本指向内置输入函数的input被赋值为split()返回的列表对象 - 第二次循环执行
input1 = input("Hello >> ")时,程序尝试将列表对象作为函数调用,触发类型错误
解决方案
将自定义变量修改为不与内置函数重名的名称即可,示例修改后代码如下:
while True: input1 = input("Hello >> ") # 把变量名从input改成其他不冲突的名称即可 input_cmd = input1.split() if input_cmd[0] == "Sup": print("HI!")
注意事项
日常编码时不要使用Python内置的函数名、类型名、关键字作为自定义变量名,比如str、int、list、dict、print等,都可能引发同类报错。
内容的提问来源于stack exchange,提问作者user16610513
相关产品推荐
相关产品推荐

