[英]I need help making my program more streamline, as I am sure where are many things I am missing
[英]I am making a todo list program where i can add my works
我正在尝试在python中创建一个待办事项列表。我想在我的字典中添加新任务,并希望它们在我打电话时出现。我怎么能这样做?
dictionary = {} # creating a empty dictionary
while True: # to run the program infinite times
qs = input('->').lower() # command?
if "todo" in qs: # check the command
p = dictionary
print(p)
elif "add" in qs: # command
i = input("what to add?") # what to add in the todo list
dictionary = i
print("Added " + i + " to your To Do list")
else:
print("There was a error!")
我试过的代码工作但不像我预期的那样。 虽然我想把我添加到词典中的所有作品都拿出来,但它只提供了一个待办事项。 那么,我该怎么办呢?
您将字典重新分配给单个项目:
dictionary = i
你想要添加到字典,大概是这样的:
n += 1
dictionary[n] = i
看起来你正在覆盖字典而不是添加新的键/值。
更改
dictionary = i
至
index += 1
dictionary[index] = i
这应该工作。 你也可以使用update()
,甚至是list []
您似乎想要使用它的方式,您可能更好地使用list
用dictionary = []
替换dictionary = {}
然后用dictionary.append(i)
替换dictionary = i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.