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


    案例题目:身高体重BMI

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

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

    最理想的体重指数是22

    1. import math
    2. height = float(input("请输入您的身高(m):"))
    3. weight = float(input("请输入您的体重(单位:kg):"))
    4. BMI = weight / math.pow(height , 2)
    5. print("您的体质指数为:" + str(BMI))
    6. if BMI < 18.5:
    7. print("轻体重")
    8. elif 18.5 <= BMI < 24:
    9. print("健康体重")
    10. elif BMI==22:
    11. print("最理想体重")
    12. elif 24 <= BMI < 28:
    13. print("超重")
    14. else:
    15. print("肥胖")

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

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

    1. d = ['admin', '123']
    2. cont = 0
    3. con = 3
    4. while 1:
    5. name = input("请输入用户名:")
    6. if name in d:
    7. break
    8. else:
    9. con -=1
    10. print("你输入的用户名不存在,请重新输入")
    11. print("还可以再输入%d次" % (con)
    12. if con ==0:
    13. cont = 1
    14. print("已退出")
    15. break
    16. count = 3
    17. while 1:
    18. if cont == 1:
    19. break;
    20. password = input("请输入密码:")
    21. if d[1] == password:
    22. print("登录成功!")
    23. break
    24. else:
    25. print("你输入的密码不正确,请重新输入")
    26. count -= 1
    27. if count == 0:
    28. print('已退出!')
    29. break
    30. print("还可以再输入%d次" % (count))

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

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

    1. alist=[]
    2. for i in range(10):
    3. s= int(input("请输入第%d个学生成绩:"%(i+1)))
    4. alist.append(s)
    5. print("总分:%d."%(sum(alist)))
    6. print("最高分:%d."%(max(alist)))
    7. print("最低分:%d."%(min(alist)))

    案例题目:词频统计

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

    hello python

    hello java very good

    java is good than python

    python is better than c++

    python java good good

    1. dic = {}
    2. print("请输入单词:")
    3. while True:
    4. s = input("")
    5. if s == "":
    6. break
    7. for ch in "!.,:*?":
    8. s = s.replace(ch," ")
    9. s = s.lower()
    10. ls = s.split()
    11. for i in ls:
    12. if i in dic:
    13. dic[i] += 1
    14. else:
    15. dic[i]=1
    16. li = list(dic.items())
    17. li.sort(key=lambda x:x[0])
    18. li.sort(key=lambda x:x[1],reverse=True)
    19. count = 0
    20. for i in li:
    21. print("{}={}".format(i[0],i[1]))
    22. count += 1
    23. if count==3:
    24. break

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

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

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

  • 相关阅读:
    深度剖析C语言指针笔试题 Ⅱ
    Sedex验厂有证书吗?
    ubuntu20单主机安装hadoop,python的简单操作
    【Leetcode】970. Powerful Integers
    MySQL主从复制
    C. Beautiful Sets of Points(找规律&杂题)
    MES系统助力注塑企业降本增效~MES系统厂商~先达智控
    js的各种循环遍历
    请简要说明 Mysql 中 MyISAM 和 InnoDB 引擎的区别
    【error】root - Exception during pool initialization
  • 原文地址:https://blog.csdn.net/oxygen23333/article/details/127771625