函数中让用户循环输入一组分数,输入结束后保存到一个列表中。
把平均分,最高分,最低分,及格人数,及格率返回出来(接收列表为参数)
让用户自己选择是否继续输入,1继续 2结束 (选择输入只能输入1或2,错误三次自动退出
python做一个分数统计器
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- Python-ZZY 2021-08-31 13:02关注
from sys import exit def statistical(scorelist): '''定义分数统计函数,把平均分,最高分,最低分,及格人数,及格率返回出来,接受列表为参数''' if scorelist == []: #如果列表是空的 return [] average_num = sum(scorelist) / len(scorelist) #数字总和除以数量得到平均分数 max_num = max(scorelist) #max函数返回最大值 min_num = min(scorelist) #min函数返回最小值 pass_num=0 #初始化及格人数 for score in scorelist: if score >= 60: pass_num += 1 #如果及格,及格人数加1 pass_rate = pass_num / len(scorelist) #及格率等于及格人数除以总人数 return [average_num, max_num, min_num, pass_num, pass_rate] #返回最后的统计数据列表 scorelist = [] wrong = 0 #输入错误的次数 def iswrong(): '''输入错误的处理''' global wrong wrong += 1 if wrong == 3: exit() #错误3次,退出程序 def reset(): '''定义清空数据的函数''' global wrong, scorelist scorelist = [] wrong = 0 while True: #无限循环 while True: score = input("请循环输入分数,输入完毕请输入y: ") if score == "y": if scorelist != []: #如果列表不是空的 print("平均分,最高分,最低分,及格人数,及格率") print(statistical(scorelist)) #就打印列表内容 break else: print("您没有输入过任何数据!") continue reset() continue elif score == "": #没有输入数据 print("您没有输入内容!") continue try: score = float(score) except ValueError: print("您输入的不是数字!") continue else: #无异常 scorelist.append(score) #将分数添加进列表 while True: goon = input("您还要继续输入吗?1继续,2退出: ") if goon == "1": break elif goon == "2": exit() else: #输入的什么也不是 print("输入不合理!达到三次退出程序") iswrong()
如果代码写的好,麻烦给个采纳!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 PADS Logic 原理图
- ¥15 PADS Logic 图标
- ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
- ¥20 气象站点数据求取中~
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
- ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
- ¥15 机器人轨迹规划相关问题