栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

关于python基础练习的小结(三)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于python基础练习的小结(三)

本周主要学习的是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)

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316166.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号