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(不是在函数中)尝试过该行,它可以工作,但是由于某种原因,它在函数内不起作用。
有人可以向我解释吗?
[C0行的问题。
您将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 = input("in or out? ")
您已使用关键字“输入”来命名变量。除非要覆盖语言的内置功能,否则切勿使用关键字来定义函数或变量。