本周主要学习的是python关于循环、条件以及一些条件句写法的部分
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 空气质量
@WELCOME CloudyTenderness
@Date 2021/10/11 14:04
@Describe python_exp
@version 1.0
"""
# 用循环判断是不是正整数
a = int(input('input a number:'))
b = 0
if a > 1:
for i in range(2, a):
if a % i == 0 & i != a:
print('不是素数!')
b = 1
break
if b == 0:
print('是素数!')
# 使用选择和循环,输出由1、2、3、4这四个数组成的每位数都不相同的所有三位数
a = [1, 2, 3, 4]
print('由1、2、3、4这四个数组成的每位数都不相同的所有三位数如下:')
for i in a:
for j in a:
for k in a:
if i != j and j != k and i != k:
print(i * 100 + j * 10 + k)
# 编写程序,计算100以内所有奇数的和
Sum = 0
for i in range(1, 101, 2):
Sum += i
# print(i)
print('Sum=',Sum)
# 配套实验指导书-实验10
while True:
try:
n = int(input('评委人数:'))
assert n > 2
break
except:
print('评委人数需要大于2!')
scores = []
for i in range(n):
while True:
try:
score = int(input('请输入第{0}个评委的分数:'.format(i+1)))
assert 0 <= score <= 100
scores.append(score)
break
except:
print('需要输入一个0-100的数')
high = max(scores)
scores.remove(high)
low = min(scores)
scores.remove(low)
final = round(sum(scores) / len(scores), 2)
formatStr = '去掉一个最高分{0}\n去掉一个最低分{1}\n最后得分{2}'
print(formatStr.format(high, low, final))
# 优化后
while True:
try:
n = int(input("请输入评委人数:"))
assert n > 2
break
except:
print("必须输入大于2的整数")
maxScore, minScore, total = 0, 100, 0
scores = []
for i in range(n):
while True:
try:
score = float(input("请输入第{0}个评委的分数".format(i + 1)))
assert 0 <= score <= 100
scores.append(score)
break
except:
print("必须输入0~100的实数")
total += score
if score > maxScore:
maxScore = score
if score < minScore:
minScore = score
finalScore = round(total - maxScore - minScore / (n - 2), 2)
high = max(scores)
low = min(scores)
formatter = "去掉一个最高分{0}\n去掉一个最低分{1}\n最后得分{2}"
print(formatter.format(high, low, finalScore))
?cut函数:
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 空气质量
@WELCOME CloudyTenderness
@Date 2021/10/11 17:50
@Describe python_exp
@version 1.0
"""
from collections import Counter
from pandas import cut
scores = [69, 89, 93, 100, 45, 24, 78, 65]
groups = Counter(cut(scores, [0, 60, 70, 80, 90, 101],
labels=['不及格', '及格', '中', '良', '优秀'],
right=False))
print(groups)
age = [3, 5, 7, 13, 67, 84, 42, 23, 12, 37, 1, 90]
ageGroup = Counter(cut(age, [0, 10, 25, 40, 60, 101],
labels=['青幼年', '青年', '中年', '中老年', '老年']))
print(ageGroup)
?
|