【发布时间】:2017-06-30 09:38:23
【问题描述】:
我正在尝试查找班级的平均数学分数,但我不确定如何在字典中执行此操作。如何使用简单的方法将每个分数相加,然后除以学生人数 (num_students)?
login="teacher"
password="school"
usrnm=input("Please enter your username: ")
pw=input("Please enter your password: ")
if (usrnm==login) and (pw==password):
print("==Welcome to the Mathematics Score Entry Program==")
num_students = int(input("Please enter number of students:"))
print ("you entered ",num_students," students")
student_info = {}
student_data = ['Maths Score: ']
for i in range(0,num_students):
student_name = input("Name :")
student_info[student_name] = {}
for entry in student_data:
student_info[student_name][entry] = int(input(entry))
print (student_info)
else:
print("No way, Jose!")
【问题讨论】:
-
虽然可以算出来。但在您的问题中包含一个您期望数据看起来如何的样本会非常有帮助。
-
通常最好将输入/输出与逻辑分开。因此,您应该编写一个函数,将字典作为输入并返回平均值,但无需所有交互。
-
请不要破坏您的帖子。发布问题后,您已将内容授权给整个 Stack Overflow 社区(根据 CC-by-SA 许可)。如果您想取消此帖子与您帐户的关联,请参阅What is the proper route for a disassociation request?。
标签: python dictionary average