首页 > 解决方案 > 表达式函数类型执行不正确

问题描述

我写了一个python小程序 输入学生成绩后 如果输入 -1 程序自动计算学生人数 学生成绩和学生平均成绩 但是程序执行时出现错误信息 error Make it possible to execute

   avg=sum1/(len(score)-1)
TypeError: object of type'int' has no len()

希望可以向大家求助 我的代码:

stu=list()
sum1=0
while True:
     score=int(input("Please enter the student's score:"))
     if score==-1:
         break
     stu.append(score)
     sum1+=score
print("Total",len(stu),"students")
avg=sum1/(len(score)-1)
print("Class total score",str(sum1),"points",", average grade:",str(avg),"points")

谢谢大家

标签: python

解决方案


你做的错误是你试图得到len你转换为整数的分数。相反,替换len(score)len(stu).

但我建议此代码的改进版本。在这里,您必须输入以空格分隔的乐谱,而不是一个接一个地输入乐谱。


student_scores = list(map(int(input("Please enter the scores of the students each separated by a space:\n"))))

average = sum(student_scores) / len(student_scores)

print(f"Class total score: {sum(student_scores)} points")
print(f"Class average score: {average} points")


推荐阅读