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