使用input()函数时出现Python错误[TypeError:'str'对象不可调用]

时间:2019-10-20 07:41:36

标签: python input callable

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(不是在函数中)尝试过该行,它可以工作,但是由于某种原因,它在该函数内不起作用。

有人可以向我解释吗?

3 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:0)

input = input("in or out? ")覆盖了内置的 input 功能。 用其他名称替换变量名称,它将起作用。

答案 2 :(得分:0)

尝试一下:

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

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

您已经使用关键字“输入”来命名变量。除非要覆盖语言的内置功能,否则切勿使用关键字来定义函数或变量。