gpt4 book ai didi

python - 嵌套根据用户输入创建的多个字典

转载 作者:行者123 更新时间:2023-12-01 01:56:19 26 4
gpt4 key购买 nike

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 个词典的词典。但是,当我尝试打印妈妈词典时,只会打印最新的词典。

最佳答案

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

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

关于python - 嵌套根据用户输入创建的多个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50141770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com