这是我试图实现的代码示例:
def notes():
print "\nPlease enter any notes:"
global texts
texts = []
if not texts:
print "no notes exist."
write_note()
else:
print "this note already exists"
def write_note():
while True:
global txt
txt = raw_input(">>> ")
if not txt:
break
else:
texts.append(txt)
print "\nNote(s) added to report."
notes_menu()
def print_note():
new_report.write("\nNotes:")
for txt in texts:
new_report.write("\n-%r" % txt)
print "Note Printed to %r. Goodbye!" % file_name
exit(0)
我的目标是在第二次(或无限期)调用“notes()”时将新输入添加到“文本”列表中并且不覆盖列表。我试图至少在调用“notes()”时确定列表是否为空。但是每次我这样做时,无论我在上次调用期间在“文本”中创建了多少项目,它总是打印“没有笔记存在”。
在这一点上,我有点不知所措。我查看了字典功能,但我不确定如何将其合并到此代码中。有人有什么建议/建议吗?