以下代码中的字典数据值将被覆盖,尽管键不同,但可能是导致覆盖的原因。
#!/usr/local/bin/python2.7
data={}
marks = {}
subjects = ["Tamil","English","Maths","Science","Social"]
for i in range(1,3):
print 'Enter marks for student%d'%i
for subject in subjects:
marks[subject] = input("Enter the " + subject + " marks: ")
data[i]={'marks':marks}
print data
Run Code Online (Sandbox Code Playgroud)
代码输出:
Enter marks for student1
Enter the Tamil marks: 10
Enter the English marks: 20
Enter the Maths marks: 30
Enter the Science marks: 40
Enter the Social marks: 50
{1: {'marks': {'Maths': 30, 'Science': 40, 'Social': 50, 'Tamil': 10, 'English': 20}}}
Enter marks …Run Code Online (Sandbox Code Playgroud)