【发布时间】:2015-11-30 11:31:15
【问题描述】:
我对python比较陌生,我是学生,我做了一个计算总分和百分比的代码,我想将9个字典加在一起,我尝试过+,combine_dict,merge_dict,我一直在尝试从最近几天开始寻找答案,但找不到任何有用的东西..代码是:
name1= input("enter the name of the first student: ")
marks = {}
subjects = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects:
marks[subject] = float(input("Enter " + name1 + "'s " + subject + " marks: "))
tota = sum(marks.values())
average = float(tota) / len(marks)
print ("\n")
name2= input("enter the name of the second student: ")
marks1 = {}
subjects1 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects1:
marks1[subject] = float(input("Enter " + name2 + "'s " + subject + " marks: "))
tota1 = sum(marks1.values())
average1 = float(tota1) / len(marks1)
print ("\n")
name3= input("enter the name of the third student: ")
marks2 = {}
subjects2 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects2:
marks2[subject] = float(input("Enter " + name3 + "'s " + subject + " marks: "))
tota2 = sum(marks2.values())
average2 = float(tota2) / len(marks2)
print ("\n")
name4= input("enter the name of the fourth student: ")
marks3 = {}
subjects3 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects3:
marks3[subject] = float(input("Enter " + name4 + "'s " + subject + " marks: "))
tota3 = sum(marks3.values())
average3 = float(tota3) / len(marks3)
print ("\n")
name5= input("enter the name of the fifth student: ")
marks4 = {}
subjects4 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects4:
marks4[subject] = float(input("Enter " + name5 + "'s " + subject + " marks: "))
tota4 = sum(marks4.values())
average4 = float(tota4) / len(marks4)
print ("\n")
name6= input("enter the name of the sixth student: ")
marks5 = {}
subjects5 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects5:
marks5[subject] = float(input("Enter " + name6 + "'s " + subject + " marks: "))
tota5 = sum(marks5.values())
average5 = float(tota5) / len(marks5)
print ("\n")
name7= input("enter the name of the seventh student: ")
marks6 = {}
subjects6 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects6:
marks6[subject] = float(input("Enter " + name7 + "'s " + subject + " marks: "))
tota6 = sum(marks6.values())
average6 = float(tota6) / len(marks6)
print ("\n")
name8= input("enter the name of the eighth student: ")
marks7 = {}
subjects7 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects7:
marks7[subject] = float(input("Enter " + name8 + "'s " + subject + " marks: "))
tota7 = sum(marks7.values())
average7 = float(tota7) / len(marks7)
print ("\n")
name9= input("enter the name of the nineth student: ")
marks8 = {}
subjects8 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects8:
marks8[subject] = float(input("Enter " + name9 + "'s " + subject + " marks: "))
tota8 = sum(marks8.values())
average8 = float(tota8) / len(marks8)
print ("\n")
name10= input("enter the name of the tenth student: ")
marks9 = {}
subjects9 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]
for subject in subjects9:
marks9[subject] = float(input("Enter " + name10 + "'s " + subject + " marks: "))
tota9 = sum(marks9.values())
average9 = float(tota9) / len(marks9)
print ("\n")
marks10= merge_dicts(marks, marks1, marks2, marks3, marks4, marks5, marks6, marks7, marks8, marks9)
print ("total marks for whole class are " + marks10 )
我想做的是在marks10中所有其他字典应该加在一起,例如
marks1=[90, 80, 90, 80, 90, 80, 90, 80]
marks2=[90, 80, 90, 80, 90, 80, 90, 80]
marks3=[90, 80, 90, 80, 90, 80, 90, 80]
marks4=[90, 80, 90, 80, 90, 80, 90, 80]
marks5=[90, 80, 90, 80, 90, 80, 90, 80]
marks6=[90, 80, 90, 80, 90, 80, 90, 80]
marks7=[90, 80, 90, 80, 90, 80, 90, 80]
marks8=[90, 80, 90, 80, 90, 80, 90, 80]
marks9=[90, 80, 90, 80, 90, 80, 90, 80]
我希望标记 10 是 标记10=[810,720,810,720,810,720,810,720] 希望现在很清楚..
【问题讨论】:
-
您的意思是更新 dict 值或在 dicts 中添加值?
-
完全不清楚“添加”字典的含义。你希望结果是什么样的?你能举一个 Python 数据结构的输入和预期输出的例子吗?另外,使用循环而不是复制相同的代码十次。
-
如果你有 100 个学生呢?您会为所有这些复制粘贴该代码吗? :) 我建议研究使用循环;您会发现生成最终标记也变得更加容易。
-
@DanielSanchez 是的,我要求将 dict 值添加到另一个 dict..
-
而我不使用循环的原因是,我对它们还不好:3 每次我在某处使用循环时,我都会搞砸。
标签: python arrays python-3.x dictionary addition