python使用for循环打印字典的键和值_如何用python打印字典键和值

我必须制作一个程序,要求用户为每个学生输入一个学生名和他们的成绩。把它们作为[键]和[值]放到字典里。当用户输入“q”时,程序退出,然后打印学生及其成绩,如下所示:

Student Grade

Student1 A

Student2 B

Student3 C

我的代码如下: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_i

你可能感兴趣的:(python使用for循环打印字典的键和值_如何用python打印字典键和值)