Python:列表字典-输入的平均回报

2024-09-30 14:38:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要考虑一个学生列表,创建一个字典,然后从一个输入,我必须返回平均从学生。

你们已经帮过我了:

我需要返回我的平均计算值,但它不起作用

StudentGrades = {
"Alcott": [5, 9, 7],
"Jerry": [3, 5, 2],
"David": [8, 9, 8, 9]
}

## I fixed this, thank you
print("Students:")
for x, y in StudentGrades.items():
    print(x, y)

while True:
    print("Select a student: ")
    name = str(input())

    if name in StudentGrades.keys():
        print("You have chosen: " + name)
        print (res = sum(StudentGrades[name]/len(StudentGrades[name]))
    else:
        print('You chosen wrong!')
       break

## Also, I didn't wrap right John's value, thank you for showing me
#use of update function

print("Original Dictionary:")
print(StudentGrades)

StudentGrades.update(student)
print("Dictionary after update:")
print (StudentGrades)

#John's Average (not working? int object is not iterable)
res = sum(StudentGrades["John"]) / len(StudentGrades["John"])
print("John's average is:[10]/1 =" + str(res))

但我也在想我是否可以退回这样的东西:

#Alcott's average
resAlcott = sum(StudentGrades["Alcott"]) / len(StudentGrades["Alcott"])


print("Students:")
for x, y in StudentGrades.items():
    print(x, y)

while True:
    print("Select a student: ")
    name = str(input())

    if name in StudentGrades.keys() == "Alcott":
        print("You have chosen: " + name + "'s average is", StudentGrades["Alcott"], resAlcott)
        
        
    else:
        print('You chosen wrong!')
        break

谢谢你,我再次感到抱歉


Tags: nameinyouforlenupdateresjohn
2条回答

你需要这样的东西:

StudentGrades = {
"Alcott": [5, 9, 7], 
"Jerry": [3, 5, 2], 
"David": [8, 9, 8, 9]
}

print("Students:")
for x, y in StudentGrades.items():
    print(x, y)
 
while True:
    print("Select a student: ")
    name = str(input())
 
    if name in StudentGrades.keys():
        print("You have chosen: " + name)
        print( sum(StudentGrades[name]) / len(StudentGrades[name]) ). # This is the part of your code that was wrong.
    else:
        print('You chosen wrong!')
        break

此外,要添加新学生,不应使用student = {'Name': 10},而应使用student = {'Name': [10]}。与所有其他值一样,该值必须是一个列表

我想你需要在这里做出改变。首先是崩溃

print (res = sum(StudentGrades[name]/len(StudentGrades[name]))

分为两行,并修复括号问题以获得

res = sum(StudentGrades[name])/len(StudentGrades[name])
print(res)

您需要进行的第二个更改是将更新的学员从

student = {'John': 10}

student = {'John': [10]}

这样就可以反复查看他的分数

StudentGrades = {
"Alcott": [5, 9, 7],
"Jerry": [3, 5, 2],
"David": [8, 9, 8, 9]
}

print("Students:")
for x, y in StudentGrades.items():
    print(x, y)

while True:
    print("Select a student: ")
    name = str(input())

    if name in StudentGrades.keys():
        print("You have chosen: " + name)
        res = sum(StudentGrades[name])/len(StudentGrades[name])
        print(res)
    else:
        print('You chosen wrong!')
        break

#Alcott's average
res = sum(StudentGrades["Alcott"]) / len(StudentGrades["Alcott"])

print("Alcott's average is:[5,9,7]/3 =" + str(res))

#Jerry's Average
res = sum(StudentGrades["Jerry"]) / len(StudentGrades["Jerry"])
limited_res = round(res,2)
print("Jerry's average is:[3,5,2]/3 =" + str(limited_res))

#David's Average
res = sum(StudentGrades["David"]) / len(StudentGrades["David"])

print("David's average is:[8,9,8,9]/4 =" + str(res))

student = {'John': [10]}

#use of update function

print("Original Dictionary:")
print(StudentGrades)

StudentGrades.update(student)
print("Dictionary after update:")
print (StudentGrades)

#John's Average (not working? int object is not iterable)
res = sum(StudentGrades["John"]) / len(StudentGrades["John"])
print("John's average is:[10]/1 =" + str(res))

相关问题 更多 >