You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python循环中用input()覆盖同名变量引发TypeError错误求助

解决input函数引发的TypeError: 'str' object is not callable错误

错误原因

你踩了命名冲突的坑:把Python内置的input()函数名当成了自定义变量名。第一次循环执行input = input().upper()时,原本的input函数被覆盖成了字符串对象。第二次循环再试图调用input()时,实际是在调用这个字符串,而字符串本身不支持被调用,所以触发了TypeError。

修复后的代码

把变量名改成非内置函数名即可,比如user_input:

while True:
    user_input = input().upper()
    text = "\n"
  
    for i in range(5):
        for char in user_input:
            text += letters[char][i]
        text += "\n"
        
    print(text)

注意事项

  • 别用Python内置函数名、关键字当自定义变量名,比如input、print、list、str这类,不然会覆盖内置对象,引发各种异常。

内容的提问来源于stack exchange,提问作者MikeyPikey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:05:11