简体   繁体   English

“str”对象不可调用,但我根本不使用“str”

[英]'str' object is not callable, but I'm not using “str” at all

I'm using Google Colab for development.我正在使用Google Colab进行开发。 My script only has one line of code where I'm supposed to read input from the console:我的脚本只有一行代码,我应该在其中读取来自控制台的输入:

question = input("Hello")

But it is throwing this error: 'str' object is not callable但它抛出了这个错误: 'str' object is not callable

I search for similar problems and all of them where related to code using "str" as a variable or function name so it needed to be renamed, but in my case it is literally one line of code and I'm not using "str" at all.我搜索了类似的问题,所有这些问题都与使用“str”作为变量或函数名的代码相关,因此需要重命名,但在我的情况下,它实际上是一行代码,我没有使用“str”根本。

Here is a screenshot of Google Colab:这是 Google Colab 的屏幕截图:

在此处输入图片说明

I tried this in a brand new notebook and it worked.我在一个全新的笔记本上试过这个,它工作正常。 In your notebook it appears that input is not the built-in function to read user input but is actually a string of some sort.在您的笔记本中, input似乎不是读取用户输入的内置函数,而是某种字符串。

To debug this further, try changing the code to just say input .要进一步调试,请尝试将代码更改为仅input Then it will print what the input identifier is bound to.然后它将打印input标识符绑定到的内容。 You ought to see something like <bound method Kernel.raw_input of <google.colab._kernel.Kernel object at 0x7f9fb5f40210>> ...您应该看到类似<bound method Kernel.raw_input of <google.colab._kernel.Kernel object at 0x7f9fb5f40210>> ...

新笔记本的截图

...but I suspect you'll see something else. ...但我怀疑你会看到别的东西。

somewhere above you have上面的某个地方你有

input = something_that_is_a_string

and that is shadowing the builtin input那是隐藏内置输入

dont name your variables input不要命名你的变量输入

You have a string variable named input previously in your code.您的代码中先前有一个名为input的字符串变量。 Or perhaps you previously had such a variable and deleted the code, but the variable is still in the namespace.或者也许您之前有这样一个变量并删除了代码,但该变量仍在命名空间中。 Just restart your Google Colab environment from the top menu: Runtime --> Restart Runtime .只需从顶部菜单重新启动您的 Google Colab 环境: Runtime --> Restart Runtime That will clear out all variables from memory and let you start over.这将从内存中清除所有变量并让您重新开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM