简体   繁体   English

使用 input() 函数时出现 Python 错误 [TypeError: 'str' object is not callable]

[英]Python Error [TypeError: 'str' object is not callable] comes up when using input() funtion

notes =[]

def newNote(notes):

    note = input("Whats up")
    notes.append(note)
    return notes

input = input("in or out? ")

if (input == "in"):

    newNote(notes)

note = input("Whats up") is the line that has the problem and I see nothing wrong with it. note = input("Whats up")是有问题的行,我认为它没有任何问题。 I have tried the line just by instelf (not in a function) and it works but for some reason it doesnt work inside the function.我已经通过 instelf (不在函数中)尝试了这条线,它可以工作,但由于某种原因,它在 function 内不起作用。

Can anyone explain this to me?谁能给我解释一下?

The problem with line input = input("in or out? ") . line input = input("in or out? ")的问题。

You redefine input function to result of input("in or out? ") , so now the input is a string.您将input function 重新定义为input("in or out? ")的结果,所以现在input是一个字符串。

The solution is to simply change input("in or out? ") result variable to something another:解决方案是简单地将input("in or out? ")结果变量更改为另一个:

notes =[]

def newNote(notes):

    note = input("Whats up")
    notes.append(note)
    return notes

choice = input("in or out? ")

if (choice == "in"):

    newNote(notes)

input = input("in or out? ") is overriding the built-in input function. input = input("in or out? ")覆盖内置输入function。 Replace the variable name with a different name and it will work.用不同的名称替换变量名称,它将起作用。

Try this:尝试这个:

notes =[]
def newNote(notes):
    note = input("Whats up")
    notes.append(note)
    return notes

inp = input("in or out? ")
if (inp == "in"):
    newNote(notes)

You have used the keyword 'input' to name a variable.您已使用关键字“输入”来命名变量。 You should never use a keyword to define functions or variables unless you want to override the built in functionality of the language.除非你想覆盖语言的内置功能,否则你不应该使用关键字来定义函数或变量。

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

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