嵌套由用户输入制作的多个词典

时间:2018-05-02 19:02:38

标签: python dictionary nested

def individualstudent():
  count=1
  for i in range (1,3):
      sname=input('Enter name for student '+str(count) +' : ')
      sID=int(input('Enter ID for student '+str(count)+' : '))
      smark=int(input('Enter mark for student '+str(count)+' : '))
      studno=('student'+str(count))
      studno={'name':sname,'ID':sID,'mark':smark}
      totaldict[count]=studno
      count+=1

  print(totaldict)
individualstudent()

我正在尝试创建一个包含200个字典的字典。 但是,当我尝试打印momma字典时,只会打印最新的字典。

1 个答案:

答案 0 :(得分:2)

您必须使用bracket表示法才能将元素添加到字典中。

totaldict = {}
for i in range (1,201):
   ........................ 
   totaldict[count] = studno