一.实验对象:《零基础学Python》第八章的3道实例和4道实战

二.实验环境:IDLE Shell 3.9.7

三.实验要求:学习使用标准模块和第三方模块

四.实验过程:

  • 实例01 创建计算BMI指数的模块

点击查看代码
def fun_bmi(person,height,weight):
'''功能:根据身高和体重计算BMI指数
person:姓名
height:身高,单位:米
weight:体重,单位:千克
'''
print(person+"的身高:"+str(height)+"米\t体重:"+str(weight)+"千克")
bmi=weight/(height*height)
print(person+"的BMI指数为:"+str(bmi))
#此处省略了显示判断结果的代码
def fun_bmi_upgrade(*person):
'''功能:根据身高和体重计算BMI指数(升级版)
*person:可变参数该参数中需要传递带3个元素的列表,
分别为姓名、身高(单位:米)和体重(单位:千克)
'''
#此处省略了函数主体代码

  • 实例02 导入两个包括同名函数的模块

点击查看代码
def girth(width,height):
'''功能:计算周长
参数:width(宽度)、height(高)
'''
return(width+height)*2
def area(width,height):
'''功能:计算面积
参数:width(宽度)、height(高)
'''
return width*height
if __name__=='__main__':
print(area(10,20))

点击查看代码
import math
PI=math.pi
def girth(r):
'''功能:计算周长
参数:r(半径)
'''
return round(2*PI*r,2)
def area(r):
'''功能:计算面积
参数:r(半径)
'''
return round(PI*r*r,2)
if __name__=='__main__':
print(girth(10))

点击查看代码
import rectangle as r
import circular as c
if __name__=='__main__':
print("圆形的周长为:",c.girth(10))
print("矩形的周长为:",r.girth(10,20))

运行结果:

  • 实例03 在指定包中创建通用的设置和获取尺寸的模块

点击查看代码
_width=800
_height=600
def change(w,h):
global _width
_width=w
global _height
_height=h
def getWidth():
global _width
return _width
def getHeight():
global _height
return _height

点击查看代码
from settings.size import *
if __name__=='__main__':
change(1024,768)
print('宽度:',getWidth())
print('高度:',getHeight())

运行结果:

  • 实例04 生成由数字、字母组成的4位验证码

点击查看代码
import random
if __name__=='__main__':
checkcode=""
for i in range(4):
index=random.randrange(0,4)
if index!=i and index+1!=i:
checkcode+=chr(random.randint(97,122))
elif index+1==i:
checkcode+=chr(random.randint(65,90))
else:
checkcode+=str(random.randint(1,9))
print("验证码:",checkcode)

运行结果:

  • 实战一:大乐透号码生成器

点击查看代码
print("大乐透号码生成器")
import random
number=input("请输入要生成的大乐透号码注数:")
n=int(number)
for i in range(n):
QQ=random.sample(range(1, 36), 5)
HQ=random.sample(range(1,13),2)
print('{:0>2d}'.format(QQ[0]),'{:0>2d}'.format(QQ[1]),'{:0>2d}'.format(QQ[2]),'{:0>2d}'.format(QQ[3]),'{:0>2d}'.format(QQ[4]),"\t",'{:0>2d}'.format( HQ[0]),'{:0>2d}'.format(HQ[1]))

运行结果:

  • 实战二:春节集五福

点击查看代码
import sys
sys.path.append(r"C:\Python\Python39\Lib")
import random
def Ji_Fu():
wf=['爱国福','富强福','和谐福','友善福','敬业福']
fu=random.sample(wf, 1)
return fu
def wf(fu):
print('当前拥有的福:')
for i, j in fu.items():
print(i,': ',j,'\t',end='')
def WuFu(fu):
type=1
for i, j in fu.items():
if j==0:
type=0
return type;
print('开始集福啦~~~')
wufu={'爱国福':0,'富强福':0,'和谐福':0,'友善福':0,'敬业福':0}
while WuFu(wufu)==0:
input('\n按下<Enter>键获取五福')
Strfu=Ji_Fu()[0]
print('获取到:' +Strfu)
wufu[Strfu] += 1
wf(wufu)
print('\n恭喜您集成五福!!!')

运行结果:

  • 实战三:封装用户的上网行为

点击查看代码
import sys
sys.path.append(r"C:\Python\Python39\Lib")
def sw(time):
print('浏览网页',str(time)+'小时')
return time
def ksp(time):
print('看视频',str(time)+'小时')
return time
def wwlyx(time):
print('玩网络游戏',str(time)+'小时')
return time
def swxx(time):
print('上网学习',str(time)+'小时')
return time
def Time(time):
if time>8:
print("今天上网时间共计"+str(time)+"小时,请保护眼睛,合理安排上网时间!")
return time
import random
person='小明'
time=0
print(person,'上网时间、行为统计:')
time+=sw(1.5)
time+=ksp(2)
time+=wwlyx(3)
time+=swxx(2)
Time(time)

运行结果:

  • 实战四:计算个人所得税

点击查看代码
import sys
sys.path.append(r"C:\Python\Python39\Lib")
def individual_income_tax(monthly_income):
'''由月收入计算个人所得税'''
Monthly_income=input("请输入月收入:")
Monthly_Income=int(Monthly_income)
Tax=Monthly_Income*(165/8000)
print('应纳个人所得税税额为{:.2f}'.format(Tax))
individual_income_tax(8000)

运行结果:

Python第八章实验报告的更多相关文章

  1. 20201123 实验二《Python程序设计》实验报告

    20201123 2020-2021-2 <Python程序设计>实验报告课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验日期 ...

  2. 20212115 实验二 《python程序设计》实验报告

    实验二 计算器设计 #20212115 2021-2022-2 <python程序设计> 实验报告二 课程: 课程:<Python程序设计>班级: 2121姓名: 朱时鸿学号: ...

  3. 20184302 实验三《Python程序设计》实验报告

    20184302 2019-2020-2 <Python程序设计>实验3报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:20184302 实验教师 ...

  4. 20201123 实验三《python程序设计》实验报告

    20201123 2020-2021-2 <python程序设计>实验三报告 课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验 ...

  5. 20201123 实验一《Python程序设计》实验报告

    20201123 2020-2021-2 <Python程序设计>实验一报告 课程:<Python程序设计> 班级:2011班 姓名:晏鹏捷 学号:20201123 实验教师: ...

  6. 20202127 实验二《Python程序设计》实验报告

    20202127 2021-2022-2 <Python程序设计>实验二报告 课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强 ...

  7. 20202127 实验一《Python程序设计》实验报告

    20202127 2022-2022-2 <Python程序设计>实验一报告课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强实 ...

  8. 20212115 实验三 《python程序设计》实验报告

    实验报告 20212115<python程序设计>实验三报告 课程:<Python程序设计>班级: 2121姓名: 朱时鸿学号:20212115实验教师:王志强老师实验日期:2 ...

  9. 20212115朱时鸿实验一《python程序设计》实验报告

    ------------恢复内容开始------------ #学号20212115 <python程序设计>实验一报告 课程: <python程序设计> 班级:2121 姓名 ...

  10. Python程序设计实验报告二:顺序结构程序设计(验证性实验)

      安徽工程大学 Python程序设计 实验报告 班级   物流191   姓名  崔攀  学号3190505136 成绩 日期     2020.3.22     指导老师       修宇 [实验 ...

随机推荐

  1. 01.BeanFactory实现

    /** beanFactory 不会做的事:* 1.不会主动调用BeanFactory 后处理器* 2.不会主动添加Bean后处理器* 3.不会主动初始化单例(懒加载)* 4.不会解析beanFact ...

  2. Windows本地文件上传到Linux服务器(腾讯云)

    环境 本地 操作系统:Window 10 企业版LTSC;内存:8GB;操作类型:64位. 服务器 CentOS 8(1核2GB,1Mbps) 64位 ,已安装Docker(CentOS 8 的doc ...

  3. Jquery_002

    6.$.ajax方法 $.ajax([options]) options是一个json格式的对象,参数是通过键值对的形式存在的 常用的参数如下: async:(默认: true) 默认设置下,所有请求 ...

  4. holiday06-英语语法-语序和五种基本句式

    第六天 英语五种基本句式: 基本句式一:S V (主+谓) 基本句式二:S V P (主+系+表) 基本句式三:S V O (主+谓+宾) 基本句式四:S V o O(主+谓+间宾+直宾) 基本句式五 ...

  5. count(1) and count(*),count(字段)区别及效率比较

    执行结果: count(1)和count(*)之间没有区别,因为count(*)count(1)都不会去过滤空值, count(字段) 会统计该字段在表中出现的次数,忽略字段为null 的情况.即不统 ...

  6. Gstreamer 随笔

    1. Gstreamer在Ubuntu上需要安装得全部库: gstreamer1.0-alsa - GStreamer plugin for ALSAgstreamer1.0-clutter-3.0  ...

  7. vue 封装时间格式化和number精确度

    //format.js 公用js /** * Parse the time to string * @param {(Object|string|number)} time * @param {str ...

  8. 全局监控Promise错误

    一.问题引入 Promise 在前端中的使用已经非常普遍了,但是许多开发者或许习惯了链式调用却忘了捕获 Promise 的错误了. 例如: function forgetCatchError () { ...

  9. 使用git钩子防止合并分支

    git是一款实用的版本管理工具,我们通过git init初始化一个git仓库,git会在当前目录为我们生成一个.git/目录,用来管理我们的版本文件信息. 在这个目录中有一个二级目录.git/hook ...

  10. ReactHooks_useState

    import { useState } from "react"; import './App.css'; function App() {   const [redBorder, ...