在Python中使用词典的平均值

时间:2017-07-03 06:55:53

标签: python dictionary average

一旦我整理了所需的输入,我试图用字典来计算平均值。你能帮忙吗?我得到的错误信息是

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==")
   print("Do you want to enter the students score? Yes/No: ")
   option = input()
   option = option.title()
   student_info = {}
   while (option != "No"):
     student_name = input("Name :")
     student_info[student_name] = {}
     score = int(input("Please enter score: "))
     student_info[student_name] = score
     print("Do you want to enter the students score? Yes/No: ")
     option = input()
     option = option.title()
average = sum(student_info[student_name]/len(student_info))
print ("The average score is ", average)
else:
    print("No way, Jose!")

代码是

{{1}}

1 个答案:

答案 0 :(得分:0)

应该是

sum(student_info.values())/len(student_info)