一. 实验目的和要求
二. 实验环境
python 3.10 64-bit
三. 实验过程
实例1
代码如下:
1 def fun_bmi(person, height,weight) : 2 '''功能:根据身高和体重计算BMI指数''' 3 print(person +"的身高: " + str(height)+ "米\t体重: " + str(weight)+ 4 "千克") 5 bmi=weight/ ( height*height) 6 7 def fun_bmi_upgrade(*person) : 8 #功能:根据身高和体重计算BMI指数(升级版) 9 # 此处省略了函数主体代码
实例2
代码如下:
1 #rectangle.py 2 def girth(width, height): 3 return (width + height) * 2 4 5 6 def area(width, height): 7 return width * heightif 8 9 10 if __name__ == '__main__': 11 print(area(10, 20))
1 #circular.py 2 import math 3 PI = math.pi 4 def girth(r): 5 return round(2*PI*r, 2) 6 7 def area(r): 8 return round(PI*r*r, 2) 9 10 if __name__ == '__main__': 11 print(girth(10))
#compute.py import rectangle as r import circular as c if __name__ == '__main__': print("圆形的周长为: ", c.girth(10)) print("矩形的周长为: ", r.girth(10, 20))
实例3
代码如下:
1 #size.py 2 _width = 800 3 _height = 600 4 def change(w,h): 5 global _width 6 _width = w 7 global _height 8 _height = h 9 10 def getwidth(): 11 global _width 12 return _width 13 14 def getHeight(): 15 global _height 16 return _height
1 #main.py 2 from settings.size import * 3 4 if __name__ =='__main__': 5 change(1024, 768) 6 print('宽度: ', getwidth()) 7 print('高度: ', getHeight())
实例4
代码如下:
import random checkcode = "" for i in range(4): index = random.randrange(0, 4) if index != i and index + 1 != i: checkcode += chr(random.randint(97, 122)) # 生成a~z中的一个小写字母 elif index + 1 == i: checkcode += chr(random.randint(65, 90)) # 生成A~Z中的一个大写字母 else: checkcode += str(random.randint(1, 9)) print("验证码:", checkcode)
实战1
代码如下:
1 import random 2 def number(): 3 a=[random.randint(1,35) for i in range(5)] 4 list1=[] 5 for i in a: 6 if i not in list1: 7 list1.append(i) 8 else: 9 i=random.randint(1,35) 10 list1.append(i) 11 list1.sort() 12 a=[str(i) for i in list1] 13 b=[random.randint(1,12) for i in range(2)] 14 o=[] 15 for i in b: 16 if i not in o: 17 o.append(i) 18 else: 19 i=random.randint(1,12) 20 o.append(i) 21 o.sort() 22 b=[str(i) for i in o] 23 c=[" "*5] 24 for i in range(5): 25 if len(a[i])==2: 26 for j in range(2): 27 if len(b[j])==2: 28 pass 29 else: 30 b[j]='0'+b[j] 31 else: 32 a[i]='0'+a[i] 33 g=a+c+b 34 for i in g: 35 print(i,end=" ") 36 print("大乐透号码生成器") 37 k=int(input("请输入要生成的大乐透号码注数:")) 38 for i in range(k): 39 number() 40 print("")
实战2
代码如下:
1 import random 2 # 抽五福的方法 3 def Ji_fu(): 4 fus = ['爱国福', '富强福', '和谐福', '友善福', '敬业福'] 5 fu = random.sample(fus, 1) 6 return fu 7 8 # 打印当前拥有的所有福 9 def fus(fu): 10 print('当前拥有的福:') 11 # 字典的键值对遍历方法 打印福卡 12 for i, j in fu.items(): 13 print(i, ':', j, '\t', end='') 14 # 判断是否集齐五福 15 def fu_ready(fu): 16 # 设置一个集齐的标识flag,等于1表示集齐了 17 flag = 1 18 for i,j in fu.items(): 19 if j == 0: # 有一个福卡的数量为0 20 flag = 0 # 标识为0,可以继续集福 21 return flag 22 print('开始集福了…………') 23 # 定义一个五福字典 保存用户的福卡 24 fu = {'爱国福': 0, '富强福': 0, '和谐福': 0, '友善福': 0, '敬业福': 0} 25 # 用一个while语句循环获取五福,直到集齐为止 26 count = 0 # 设置一个计数器,记录集福的次数 27 while fu_ready(fu) == 0: # 集齐标识为0 28 input("\n按下<enter>键获取福卡:") 29 getfu = Ji_fu()[0] # 进行抽卡 30 print('获取了:', getfu) 31 fu[getfu] += 1 # 将抽到的卡加入五福字典,相应数量+1 32 fus(fu) # 调用方法打印当前拥有的五福 33 fu_ready(fu) # 调用方法判断是否集齐 34 count +=1 35 print("\n***五福已经集齐,真是有福之人***") 36 print("一共集福",count,'次')
实战3
代码如下:
1 # 网页浏览时间统计 2 def web(time): 3 print("浏览网页:"+str(time)+"小时;") 4 return time 5 # 看视频时间统计 6 def video(time): 7 print("看视频:"+str(time)+"小时;") 8 return time 9 # 玩网络游戏时间统计 10 def playgame(time): 11 print("玩网络游戏:"+str(time)+"小时;") 12 return time 13 # 上网学习时间统计 14 def study(time): 15 print("上网学习:"+str(time)+"小时。") 16 return time 17 # 合计上网时间统计 18 def total(time): 19 print('今天上网时间共计:'+str(time)+"小时,",end=' ') 20 if time >= 8: 21 print("请保护眼睛,合理安排上网时间!") 22 else: 23 print("比较合理,但也要注意休息!") 24 25 26 name = '小明' 27 print(name, '上网时间、行为统计……') 28 t1 = web(1.5) 29 t2 = video(2) 30 t3 = playgame(3) 31 t4 = study(2) 32 time = t1 + t2 + t3 + t4 33 total(time)
实战4
代码如下:
1 # 根据月收入算个税模块 2 def tax(monthmoney): 3 # 最低起征点3500 4 ds = 3500 5 baoxian = 7662 6 # 养老保险 7 yanglao = monthmoney * 0.08 8 # 医疗保险 9 yiliao = monthmoney * 0.02 10 # 失业保险 11 shiye = monthmoney * 0.005 12 # 公积金 13 housem = monthmoney * 0.12 14 # 计算三险一金总额,如超过上限按上限算 15 demoney = yanglao + yiliao + shiye + housem 16 if demoney >= 7662: 17 demoney = 7662 18 # 计算应征税额 19 lastmoney = monthmoney - demoney - ds 20 # 按不同档次征税,定义税额taxes 21 if lastmoney <= 0: 22 taxes = 0 23 elif 0< lastmoney < 1500: 24 taxes = lastmoney * 0.03 25 elif 1500 <= lastmoney < 4500: 26 taxes = lastmoney * 0.1 - 105 27 elif 4500 <= lastmoney < 9000: 28 taxes = lastmoney * 0.2 - 555 29 elif 9000 <= lastmoney < 35000: 30 taxes = lastmoney * 0.25 - 1005 31 elif 35000 <= lastmoney < 55000: 32 taxes = lastmoney * 0.3 - 2002 33 elif 55000 <= lastmoney < 80000: 34 taxes = lastmoney * 0.35 - 5505 35 elif 80000 <= lastmoney: 36 taxes = lastmoney * 0.45 - 13505 37 # if lastmoney < 0: 38 # taxes = 0 39 return taxes 40 41 42 m = float(input("请输入您的月收入总额:")) 43 taxes = tax(m) 44 print('您应征个人所得税金额为:{:.2f} 元'.format(taxes))
标签:__,fu,第十,Python,random,time,print,def From: https://www.cnblogs.com/yisheng8/p/16921820.html