6.成绩统计(15)
-
对给定人数的成绩输出其及格率和优秀率(百分号前保留整数)
-
n = int(input()) a,b=0,0 for i in range (n): s = int(input()) if s>=60: a+=1 if s>=85: b+=1 print('{:.0%}'.format(a/n)) print('{:.0%}'.format(b/n))
-
结果
- ps
1.截取字符串:'{:.5}'.format('Hello Chen') # 截取前5个字符-----Hello
2.数字格式化:print("{:.2f}".format(3.1415926))-----输出3.14
3.使用逗号作为千位分隔符:print('{:,}'.format(1234567890))-----输出'1,234,567,890'
4.表示一个百分比:print(' {:.2%}'.format(0.61898))------输出61.90%
5.时间格式化:import datetime
d = datetime.datetime(2018, 7, 31, 15, 58, 58)
print('{:%Y-%m-%d %H:%M:%S}'.format(d))----输出2018-07-31 15:58:58