【Python养成】:案例(身高体重BMI值、模拟用户登录系统、键盘录入10个学生的成绩,计算出最高分、最低分和成绩总和、词频统计)

案例题目:身高体重BMI值

        计算成人身高体重指数BMI值。公式:bmi = 体重 / (身高 * 身高),体重的单位是千克,身高的单位是米 。键盘输入身高和体重值,计算bmi值,并根据结果对用户做出友好提示,健康标准可以参考下图。

成人BMI数值
轻体重BMI<18.5
健康体重18.5<=BMI<24
超重24<=BMI<28
肥胖28<=BMI

最理想的体重指数是22

import math

height = float(input("请输入您的身高(m):"))
weight = float(input("请输入您的体重(单位:kg):"))
BMI = weight / math.pow(height , 2)
print("您的体质指数为:" + str(BMI))
if BMI < 18.5:
    print("轻体重")
elif 18.5 <= BMI < 24:
    print("健康体重")
elif BMI==22:
    print("最理想体重")
elif 24 <= BMI < 28:
    print("超重")
else:
    print("肥胖")

案例题目:模拟用户登录系统

        模拟用户登录系统。程序启动提示用户输入用户名和密码,假设用户名为“admin”,密码为“123”为正确,其他输入均视为错误!系统允许用户最多输入三次。如果用户三次输入均为错误的,则程序作出温馨提示并退出!

d = ['admin', '123']
cont = 0
con = 3
while 1:
    name = input("请输入用户名:")
    if name in d:
        break
    else:
        con -=1
        print("你输入的用户名不存在,请重新输入")
        print("还可以再输入%d次" % (con)
    if con ==0:
        cont = 1
        print("已退出")
        break
count = 3
while 1:
    if cont == 1:
        break;
    password = input("请输入密码:")
    if d[1] == password:
        print("登录成功!")
        break
    else:
        print("你输入的密码不正确,请重新输入")
        count -= 1
        if count == 0:
            print('已退出!')
            break
        print("还可以再输入%d次" % (count))

案例题目:键盘录入10个学生的成绩,计算出最高分、最低分和成绩总和

        键盘录入10个学生的成绩,计算出最高分、最低分和成绩总和。【要求使用列表实现】

alist=[]
for i in range(10):
    s= int(input("请输入第%d个学生成绩:"%(i+1)))
    alist.append(s)

print("总分:%d."%(sum(alist)))
print("最高分:%d."%(max(alist)))
print("最低分:%d."%(min(alist)))

案例题目:词频统计

        词频统计:统计出下段文本中每个英文单词出现的次数,并输出出现频率最高的三个单词及其出现次数。

hello python

hello java very good

java is good than python

python is better than c++

python java good good

dic = {}
print("请输入单词:")
while True:
    s = input("")
    if s == "":
        break
    for ch in "!.,:*?":
        s = s.replace(ch," ")
    s = s.lower()
    ls = s.split()
    for i in ls:
        if i in dic:
            dic[i] += 1
        else:
            dic[i]=1
li = list(dic.items())
li.sort(key=lambda x:x[0])
li.sort(key=lambda x:x[1],reverse=True)
count = 0
for i in li:
    print("{}={}".format(i[0],i[1]))
    count += 1
    if count==3:
        break

第二种方法 创建文本 打开文本拿出文本信息,进行处理:

        当前文件夹中创建 this.txt  将文本信息存入其中

import re

# 题目要求文本 于是我采用了文本打开读取  在文件夹中有文本的数据  请老师打开查看
with open("this.txt", "r", encoding="utf-8") as fd:
    list = [] 
    dict = {} 
    for line in fd.readlines():
        for word in line.strip().split(" "):
            list.append(re.sub(r"[.|!|,]", "", word.lower()))
    sets = list(set(word_list)) 
    dict = {word: list.count(word) for word in sets if word}
result = sorted(word_dict.items(), key=lambda d: d[1], reverse=True)[:10]
print(result[:3])

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值