【问题标题】:Input from keyboard is throwing type error键盘输入引发类型错误
【发布时间】:2019-05-15 14:47:24
【问题描述】:

我正在尝试做最基本的事情,即尝试通过输入功能从键盘读取。

我尝试在提供的其他链接中查看其他解决方案。但这对我没有帮助。因为那些是先前声明输入的那些。 它给我带来了错误:-

  File "<ipython-input-169-5d707bffda8e>", line 1, in <module>
    a = input("Enter the number")

TypeError: '_io.TextIOWrapper' object is not callable

我尝试使用“输入”从键盘获取输入

a = input("Enter the number")


TypeError: '_io.TextIOWrapper' object is not callable

【问题讨论】:

  • 你似乎用什么东西覆盖了input...
  • 我使用的是 python 3.x 版本。输入未在代码中的其他任何地方使用。 'a' 不是文件
  • 你能在产生错误的那一行前面加上一个print(input),然后把它打印出来吗?如果您发布整个代码,而不仅仅是一行代码,帮助您也会容易得多。
  • @Boris - 当我尝试打印(输入)时。它给了我以下信息:-print(input) <_io.textiowrapper name="sample.txt" mode="r" encoding="cp1252">

标签: python python-3.x


【解决方案1】:

你有这样一行

with open('sample.txt') as input:

在您的代码中某处,这意味着您已经覆盖了内置的input 函数。

with open 行(以及任何使用input 作为文件对象的行,如for line in input:),您需要将input 更改为其他变量名称,如infile 或只是f .

【讨论】:

  • 代码没有 open('sample.txt') as input: 。但是看起来 Ipython 控制台存储了变量输入,并且由于相同的原因它在那里不起作用。当我重新启动时,它工作了
  • @Aditya 我不认为 ipython 重新定义了input。你的意思是你执行了一些覆盖input的代码?您可以启动另一个 ipython 会话并向我们展示%history -g -f ipython_history.txt 的输出。除非您以交互模式执行脚本或从具有类似行的本地库进行全局导入,否则您很有可能会在历史记录中找到该行。
猜你喜欢
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
相关资源
最近更新 更多