需要帮助打印列表 [英] Need help printing list
问题描述
我正在用 Python 处理待办事项列表,目前我一直在打印待办事项列表.
I'm working on a todo list in Python and I am currently stuck on printing the todo list.
我有我的添加代码和查看代码:
I have my add code and view code as such:
if sel == '1': #add task
name = input("enter task name: ")
prio = input("enter priority level (High | Medium | Low): ")
add(todo, name, prio)
view(task)
elif sel == '3': #print todo list
view2(task)
exit
这些是我使用的函数:
def add(todo, x, y):
todo[x] = y
def view(x):
x.append(dict(todo))
def view2(x):
print(x)
第一次选择打印列表选项时,我可以将项目添加到字典中打印字典.我第二次打印它会给出以下输出
I'm able to add items to a dictionary print the dictionary the first time i select the option to print list. The second time i print it gives the below output
[{'test 1': 'low'}, {'test 1': 'low', 'test 2 ': 'low'}]
我的最终目标是获得如下所示的输出,但我无法通过第一堵墙.有什么建议吗?另外,我是否需要打印嵌套字典才能获得编号列表?
my ultimate goal is to get an output as shown below, but i can't get passed this first wall. Any advice? also, would i need to print a nested dictionary to get a numbered list?
1 - test 1 - low
2 - test 2 - low
提前致谢!
推荐答案
假设 view2(x) 是打印函数,x 是你在问题中提到的字典.
Assuming view2(x) is the function for printing and x is the dictionary which you have mentioned in the question.
你可以这样做
def view2(x):
for key, val in x.items():
print("{0} - {1}".format(key, val))
这篇关于需要帮助打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!