简体   繁体   English

如何在python中打印字典键和值

[英]How to print dictionary keys and values in python

I have to make a program which asks the user to enter a student name, and their grade, for every student.我必须制作一个程序,要求用户为每个学生输入学生姓名和成绩。 And put them as [keys] and [values] in a dictionary.并将它们作为 [keys] 和 [values] 放入字典中。 And when the user enters 'q' the program quits and then prints the students and their grades such as this:当用户输入“q”时,程序退出,然后打印学生和他们的成绩,如下所示:

Student Grade
Student1 A
Student2 B
Student3 C

My code looks like this:我的代码如下所示:

student_grades = {}

while True:

    ## Get the Name [key in dictionary] of the student
    name = raw_input("Please give me the name of the student (press q to quit): \n")

    if name == 'q':
        print "Okay, printing grades! \n"
        for x in student_grades:
            print student_grades.keys() + student_grades.values()
        break

    # Chcek if name isn't valid
    elif name.isdigit():
        print "Sorry, {} isn't valid".format(name)
        name = raw_input("Please give me the name of the student (press q to quit): \n")

    ## Get the grade [value in dictionary] of the student
    grade = raw_input("Please give me their grade: (e.g 'A', 'B', 'C', 'D', 'F') \n")

    # Check if grade isn't valid
    if not grade in ['A', 'B', 'C', 'D', 'F']:
        print "Sorry, {} isn't valid".format(grade)
        grade = raw_input("Please give me their grade: (e.g 'A', 'B', 'C', 'D', 'F') \n")
        # Assign Name to Grade
        student_grades[name] = grade

When I enter 'q' the program should print the keys and values which are the student name and their grade, but it prints nothing当我输入“q”时,程序应该打印学生姓名和成绩的键和值,但它什么也不打印

You can access the keys and values of a dictionary like this:您可以像这样访问字典的键和值:

for k, v in mydict.iteritems():
    print(k, v)

In Python 3 this is changed/bettered to:在 Python 3 中,这被更改/改进为:

for k, v in mydict.items():
    print(k, v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python的列表中打印字典的键和值 - How to print keys and values of a dictionary which is contained in a list in Python 如何在 Python 中为以下代码打印字典中的前 5 个键和值? - How to print first 5 keys and values from dictionary in Python for below code? 如何从Python中的嵌套字典中一起打印所有键和值? - How to print all the keys and values together from a nested dictionary in Python? Python字典打印所有键的所有值 - Python dictionary print all values for all keys Python:具有多个值的键的字典。 如何将字典打印为分隔值列表的字典集 - Python: dictionary with keys having multiple values. How to print dictionary as set of dictionaries separating the list of values 如何从python字典打印2个键 - How to print 2 keys from a python dictionary 如何提取键及其值并添加值以在 python 字典中打印总数? - How to extract keys with its values and add values to print the total in python dictionary? 从字典打印键和值 - print keys and values from dictionary 从 Python 中的嵌套字典中按顺序排序和打印键和值 - Sort and print sequentially keys and values from a nested dictionary in Python 给定键适合特定间隔,如何在python中打印出字典的值? - How can I print out the values of a dictionary in python given the keys fit a certain interval?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM