python - 表达式函数类型执行不正确
问题描述
我写了一个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")
谢谢大家
解决方案
你做的错误是你试图得到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")
推荐阅读
- kubernetes - K8S:运行负载均衡器同步例程时出错
- constants - 测试模式的 Windev 常数
- protocol-buffers - 包预期,有语法
- java - Google drive api v3 返回文件夹总数
- c# - SQL中单引号和双引号有什么用?
- android - 来自 webview 的 Android Studio Javascript POST 更改为 GET
- android - 如何正确调整 web 视图中的图像大小
- android - Livedata在junit测试中返回null
- bixby - 如何在 bixby 中执行自由文本搜索
- java - LibGDX:使用 AIDE 解析 json