Python:成绩分类
2022/3/21 20:28:18
本文主要是介绍Python:成绩分类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
输入一组用百分制(0-100之间的整数)表示的成绩列表,每个成绩用空格隔开,将其转换为等级制并进行各等级的统计。
0<=分数<60为F 60<=分数<70为D
70<=分数<80为C
80<=分数<90为B
90<=分数<=100为A
先介绍第一种方法:
str=list(input().split()) str2={} print(str) a='A:' b='B:' c='C:' d='D:' f='F:' str2[a]=str2.get(a,0) str2[b]=str2.get(b,0) str2[c]=str2.get(c,0) str2[d]=str2.get(d,0) str2[f]=str2.get(f,0) for item in str: item=float(item) if 0<item and item<60: x='F:' str2[x]=str2.get(x,0)+1 if 60<= item and item <70: x='D:' str2[x] = str2.get(x, 0) + 1 if 70<=item and item<80: x='C:' str2[x]=str2.get(x,0)+1 if 80<=item and item<90: x='B:' str2[x]=str2.get(x,0)+1 if 90<=item and item<100: x='A:' str2[x]=str2.get(x,0)+1 str2= sorted(str2.items(),key=lambda x:x[0]) str2={k:v for k,v in str2} print(str2) for a,b in str2.items(): print(a,':'," ",b)
这里面用到了数据字典,然后将数据字典进行了排序:
str2= sorted(str2.items(),key=lambda x:x[0])
再将排序后的列表转换成字典样式
str2={k:v for k,v in str2}
第二种方法:
str = input() str = str + ' ' sum=0 flag = 0 dj=[0,0,0,0,0] for x in str : if x==' ' and flag == 1: if sum >= 90 and sum <= 100: dj[0]+=1 elif sum >= 80 and sum < 90 : dj[1]+=1 elif sum >= 70 and sum < 80 : dj[2]+=1 elif sum >= 60 and sum < 70 : dj[3]+=1 elif sum >= 0 and sum <60 : dj[4]+=1 sum = 0 flag = 0 elif x != ' ' : a=int(x) sum=sum*10+a flag = 1 print("A: %d"%dj[0]) print("B: %d"%dj[1]) print("C: %d"%dj[2]) print("D: %d"%dj[3]) print("F: %d"%dj[4])
第三种方法:
thisdict={"A": 0,"B": 0,"C": 0,"D": 0,"F": 0} a=list(map(int,input().split())) for x in a: if 90<=x<=100: thisdict["A"]=thisdict.get("A")+1; elif 80<=x<90: thisdict["B"]=thisdict.get("B")+1; elif 70<=x<80: thisdict["C"]=thisdict.get("C")+1; elif 60<=x<70: thisdict["D"]=thisdict.get("D")+1; elif 0<=x<60: thisdict["F"]=thisdict.get("F")+1; for x, y in thisdict.items(): print("%c: %d"%(x,y))
这篇关于Python:成绩分类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门