我正在开发一个ToDo应用程序,需要将一系列句子和单词保存到.txt文件中。我已经做了一些研究,但没有找到任何能够很好地解释它的教程,以便我能够理解它。正如我所说,我正在使用Python 3。以下是代码:
# Command line TO-DO list
userInput = None
userInput2 = None
userInput3 = None
todo = []
programIsRunning = True
print("Welcome to the TODO list made by Alex Chadwick. Have in mind
that closing the program will result in your TODO"
" list to DISAPPEAR. We are working on correcting that.")
print("Available commands: add (will add item to your list); remove
(will remove item from your list); viewTODO (will"
" show you your TODO list); close (will close the app")
with open('TODOList.txt', 'r+') as f:
while programIsRunning == True:
print("Type in your command: ")
userInput = input("")
if userInput == "add":
print("Enter your item")
userInput2 = input("")
todo.append(userInput2)
continue
elif userInput == "viewTODO":
print(todo)
continue
elif userInput == "remove":
print(todo)
userInput3 = input("")
userInput3 = int(userInput3)
userInput3 -= 1
del todo[userInput3]
continue
elif userInput == "close":
print("Closing TODO list")
programIsRunning = False
continue
else:
print("That is not a valid command")
programIsRunning == False
是一种比较,而不是赋值。你可能想要将False
赋值给你的变量,所以请修正为programIsRunning = False
。你最好在开始时将数据读入列表结构中,然后操作该列表以添加/删除待办事项,最后在关闭时将列表保存到文件中。 - Patrick Artner