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

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") 是有问题的行,我看不出有什么问题。我已经通过 instelf(不是在函数中)尝试了这条线并且它有效但由于某种原因它在函数内部不起作用。

谁能给我解释一下?

input = input("in or out? ") 行的问题。

您将 input 函数重新定义为 input("in or out? ") 的结果,因此现在 input 是一个字符串。

解决方案是简单地将 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? ") 正在覆盖内置的 input 函数。 用不同的名称替换变量名称,它将起作用。

试试这个:

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

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

您已使用关键字 'input' 命名变量。除非您想覆盖语言的内置功能,否则永远不要使用关键字来定义函数或变量。