Python Victor-DAY 3 列表、循环和判断语句
初始化两个变量:excellent_count 用于记录分数大于等于 90 的个数,初始值为 0;total_score 用于累加所有分数,初始值为 0。定义一个包含整数的列表 scores,赋值为 [85, 92, 78, 65, 95, 88]。循环结束后,计算平均分 average_score(总分除以分数的个数)。使用 for 循环遍历 scores 列表中的每一个分数。将当前分数累加到
·
题目:
- 定义一个包含整数的列表 scores,赋值为 [85, 92, 78, 65, 95, 88]。
- 初始化两个变量:excellent_count 用于记录分数大于等于 90 的个数,初始值为 0;total_score 用于累加所有分数,初始值为 0。
- 使用 for 循环遍历 scores 列表中的每一个分数。
- 在循环内部:
- 将当前分数累加到 total_score 变量上。
- 使用 if 语句判断当前分数是否大于等于 90。如果是,则将 excellent_count 变量加 1。
- 循环结束后,计算平均分 average_score(总分除以分数的个数)。
- 使用 f-string 分三行打印出以下信息:
- 优秀分数(>=90)的个数。
- 所有分数的总和。
- 所有分数的平均分(结果包含3位小数)。
scores=[85,92,78,65,95,88] excellent_count=0 total_score=0 for score in scores: total_score+=total_score if total_scores>=90: excellent_count+=1 average_score=total_score/len(scores) print(f"优秀分数(>=90)的个数:{excellent_count}") print(f"所有分数的总和:{total_score}") print(f"所有分数的平均分(结果包含3位小数):{average_score}")或者
scores=[85,92,78,65,95,88] excellent_count=0 total_score=0 for scores in scores: total_score+=total_score if total_scores>=90: excellent_count+=1 average_score=total_score/int(scores) print(f"优秀分数(>=90)的个数:{excellent_count}") print(f"所有分数的总和:{total_score}") print(f"所有分数的平均分(结果包含3位小数):{average_score}")
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)