继续之前的练习

本章首先介绍了如何编写和调用void函数,展示了使用函数对程序进行模块化的好处,并讨论了自顶向下的设计方法。然后,介绍如何将参数传递给函数,讨论了常用的函数库、如生成随机数的函数。接下来介绍如何使用模块来组织函数。本章最后讨论了如何使用函数模块化机器龟图形库代码。

#1公里转换器
def main():km=float(input("Please enter the number of Kilometers:"))print("The number of Miles is",km*0.6214)
if __name__=="__main__":main()
#2重构消费税结构
purchase_amount=float(input("Please entry purchase amount:"))
state_consumption_tax_rate=0.05
national_consumption_tax_rate=0.025
def total_consuption_tax():return purchase_amount*state_consumption_tax_rate+purchase_amount*national_consumption_tax_rate
def total_consuption_amount():purchase_amount+total_consuption_tax
def state_consumption_tax():return purchase_amount*state_consumption_tax_rate
def national_consumption_tax():return purchase_amount*national_consumption_tax_rate
print("Purchase amount:",purchase_amount)
print("State consuption tax:",state_consumption_tax())
print("National consuption tax:",national_consumption_tax())
print("Total consuption tax:",total_consuption_tax())
print("Total consuption amount:",total_consuption_amount())
#3多少保险
def main():cost=float(input("Please enter property replacement cost:"))print("The minimum cost of property purchase amount:",cost*0.8)
if __name__=="__main__":main()
#4汽车成本
def one_month_cost(loan,insurance,gas,oil,tyre,maintance_cost):return loan+insurance+gas+oil+tyre+maintance_cost
def main():total=0for i in range(1,13):print("The",i,"month's total cost:")loan=float(input("Please enter the loan:"))insurance = float(input("Please enter the insurance:"))gas = float(input("Please enter the gas:"))oil = float(input("Please enter the oil:"))tyre = float(input("Please enter the tyre:"))maintance_cost = float(input("Please enter the miantance cost:"))x=one_month_cost(loan, insurance, gas, oil, tyre, maintance_cost)print("The",i,"month's total cost is",x)total+=xprint("The total cost for one year is",total)
if __name__=="__main__":main()
#5财产税
def evaluation_value(real_value):return real_value*6000/10000
def property_tax(e_value):return e_value*43.2/6000
def main():value=float(input("Please enter the practical value:"))print("The evaluation is",evaluation_value(value))print("The property tax is",property_tax(evaluation_value(value)))
if __name__=="__main__":main()
#6脂肪和碳水化合物的卡路里
def calories_from_fat(fat_grams):return fat_grams*9
def calories_from_carbs(carb_grams):return carb_grams*4
def main():fg=float(input("Please enter fat grams:"))cg=float(input("Please enter carb grams:"))print("The calories from fat is",calories_from_fat(fg))print("The calories from carbs is",calories_from_carbs(cg))
if __name__=="__main__":main()
#7体育场座位
def main():A=int(input("Please enter the number of seats sold for seat type A:"))B=int(input("Please enter the number of seats sold for seat type B:"))C=int(input("Please enter the number of seats sold for seat type C:"))print("Sales revenue is",A*20+B*15+C*10)
if __name__=="__main__":main()
#8油漆作业估算器
def main():S=float(input("Please enter the number of square feet of painted walls:"))P=float(input("PLease enter the price per gallon of paint:"))if S%112==0:ans=S/112else:ans=S//112+1print("Gallons of paint required:",ans)print("Labor time required:",S*8/112)print("Cost of painting:",ans*P)print("Labor cost:",35*S*8/112)print("The total cost:",35*S*8/112+ans*P)
if __name__=="__main__":main()
#9月销售税
def main():n=float(input("Please enter total sales of the month:"))print("Amount of district/county sales tax is",n*0.025)print("Amount of states sales tax:",n*0.05)print("Total sales tax amount:",n*(0.025+0.05))
if __name__=="__main__":main()
#10英尺转换为英寸
def feet_to_inches(feet):return feet/12
def main():feet=float(input("Please enter a number for feet:"))print("The according inches is",feet_to_inches(feet))
if __name__=="__main__":main()
#11数学检测
import random
def main():a=random.randint(100,999)b=random.randint(100,999)print(" ",a)print("+",b)print("-------------------------")ans=int(input(""))if ans==a+b:print("You are right!")else:print("The correct ans is",a+b)
if __name__=="__main__":main()
#12两个数的最大值
def max(a,b):if a==b:return Noneif a>b:return aelse:return b
def main():print("Please enter two number to compare the size:")a=int(input("The first one:"))b=int(input("The next one:"))if max(a, b) is None:print("They are equal")else:print("The bigger is",max(a,b))
if __name__=="__main__":main()
#13下落距离
def falling_distance(second):return 0.5*9.8*second**2
def main():print("Falling time in second\tfalling distance")print("----------------------------------------")for i in range(1,11):print(i,"\t","%.2f"%falling_distance(i))
if __name__=="__main__":main()
#14动能
def kinetic_energy(m,v):return 0.5*m*v**2
def main():m=float(input("Please enter the weight in Kg:"))v=float(input("Please enter the spped in v/s"))print("The KE is","%.2f"%kinetic_energy(m,v))
if __name__=="__main__":main()
#15考试的平均成绩和等级
def calc_average(n1,n2,n3,n4,n5):return (n1+n2+n3+n4+n5)/5
def determine_grade(n):if 90 <= n <= 100:return 'A'elif n>=80:return 'B'elif n>=70:return 'C'elif n>=60:return 'D'else:return 'F'
def main():print("Please enter the grade of five courses:")n1=float(input("The grade of first course:"))n2 = float(input("The grade of second course:"))n3 = float(input("The grade of third course:"))n4 = float(input("The grade of fourth course:"))n5 = float(input("The grade of fifth course:"))ave=calc_average(n1, n2, n3, n4, n5)print("The average grade is",ave)print("The level is",determine_grade(ave))
if __name__=="__main__":main()
#16奇/偶计数器
import random
def is_odd_or_even(n):return n%2
def main():for i in range(100):x=random.randint(0,1000)if is_odd_or_even(x)==1:print(x,"is odd")else:print(x,"is even")
if __name__=="__main__":main()
#17素数
def is_prime(n):if n==2:return Trueif n<2:return Falseif n%2==0:return Falsei=3while i*i<=n:if n%i==0:return Falsei+=2return True
def main():x=int(input("Please enter a number:"))if is_prime(x):print(x,"is prime number")else:print(x,"isn't prime number")
if __name__=="__main__":main()
#18素数列表
def is_prime(n):if n==2:return Trueif n<2:return Falseif n%2==0:return Falsei=3while i*i<=n:if n%i==0:return Falsei+=2return True
def main():for i in range(1,101):x=iif is_prime(x):print(x,"is prime number")else:print(x,"isn't prime number")
if __name__=="__main__":main()
#19未来价值
def the_f(p,i,t):return p*((1+i)**t)
def main():p=float(input("Please enter the present value of accounts:"))i=float(input("Please enter the monthly interest rate:"))i/=100t=int(input("Please enter the number of months:"))print("Future value of the account:","%.2f"%the_f(p,i,t))
if __name__=="__main__":main()
#20随机数猜谜游戏
import random
def main():y=random.randint(1,100)x=int(input("Please enter the number you guessed:"))while x!=y:if x<y:print("Too low,try again")elif x>y:print("Too high,try again")else:breakx = int(input("Please enter the number you guessed again:"))print("Congratulations!")
if __name__=="__main__":main()
#21石头、剪刀、布游戏
import random
def player_is_win(y,x):if (y==1 and x==2)or(y==2 and x==3)or(y==3 and x==1):return Trueelse:return False
def main():#1-stone 2-cloth 3-scissorsy=random.randint(1,3)x=int(input("Please enter your choice:"))while x==y:print("It ends in a draw!")y = random.randint(1, 3)x = int(input("Please enter your choice again:"))if player_is_win(y,x):print("Player win!")else:print("Computer win!")
if __name__=="__main__":main()

Python程序设计基础(第五章函数 练习记录)相关推荐

  1. Python程序开发——第五章 函数

    目录 一.函数的定义 (一)def关键字定义函数 (二)形参和实参 (三)全局变量和局部变量 二.函数的调用 三.函数的参数传递 (一)必需参数 (二)关键字参数 (三)默认参数 (四)不定长参数 1 ...

  2. Python程序设计基础第七章笔记:字符串

    Python程序设计基础笔记 目录 Python程序设计基础笔记 第七章:文本处理(一):字符串 7.1 字符串编码格式简介 7.2 转义字符与原始字符串 7.3 字符串格式化 7.3.1 使用 % ...

  3. python - 啃书 第五章 函数

    函数定义和调用 函数定义 函数是:组织好的,可重复使用的,用来实现单一或相关联功能的代码段. 在程序中,函数的使用能提高应用的模块性.代码的重用率和可读性. 自定义函数的一般格式为: def 函数名( ...

  4. python程序设计基础 实验五

    ⭐python实验专栏,欢迎订阅与关注! ★观前提示:本篇内容为python程序设计实验,代码内容经测试没有问题,但是可能会不符合每个人实验的要求,因此以下内容建议仅做思路参考. 一.实验目的 (1) ...

  5. Python程序设计基础第一章笔记:Python概述

    Python程序设计基础笔记 目录 Python程序设计基础笔记 第一章:python概述 1.1 python是这样一种语言 1.2 python版本之争 略 1.3 python编程规范与代码优化 ...

  6. python程序设计基础课后答案-清华大学出版社-图书详情-《Python程序设计习题解析》...

    前言 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言,被列入LAMP(Linux.Apache.MySQL以及Pthon/Perl/PHP),Python语言将代码书写缩进作为语法 ...

  7. python语言是开源的现拥有十几万个第三方函数库_智慧树知到《Python程序设计基础》章节测试答案...

    智慧树知到<Python程序设计基础>章节测试答案 绪论 1.学好Python程序设计要注意的几个问题是 A:多看多练 B:多想多整理 C:多交流多请教 D:只看视频,不思考.不练习 答案 ...

  8. 【python第五章——函数】

    python第五章--函数 第五章--函数 5.1函数概述 5.2 函数进阶 下面部分之后再写 5.3函数习题(一些小例子) 第五章--函数 5.1函数概述 快速入门: #定义函数 def func( ...

  9. 非计算机专业《Python程序设计基础》教学参考大纲

    <Python程序设计基础> 教  学  大  纲 目   录 一. 课程简介 二. 教学目的和要求 三. 教学中应注意的问题 四. 教学内容 五. 教学课时分配 六. 教材与参考书目 一 ...

最新文章

  1. Java单元测试的意义_单元测试重要意义及方法介绍
  2. 艾伟:C#对游戏手柄的编程开发-API篇(2)
  3. 修改ESP8266-01S MicroPython下载固件,看是否能够烧录程序
  4. MySQL客户端和服务器端工具集
  5. 使用 Arthas 排查 SpringBoot 诡异耗时的 Bug
  6. 2014 ACM/ICPC 鞍山赛区网络赛(清华命题)
  7. 笔记本用无线路由器上网设置教程攻略
  8. 易语言多级指针读取_C语言指针难吗?纸老虎而已,纯干货讲解(附代码)
  9. adb remount overlayfs的说明
  10. 测试攻城狮必备技能点!一文带你解读DevOps下的测试技术
  11. Python 爬虫---(4) 正则的基本使用
  12. mysql date 默认值_通过Oracle DB了解MySQL
  13. Linux虚拟内存管理 | 虚拟地址与物理地址映射、段错误SIGSEGV
  14. python3.7降级3.6_请问一下Mac python3.7.1怎么降低到3.6版本?
  15. 【翻译】Adaptive Convolutions for Structure-Aware Style Transfer
  16. C#中路径表示\ 和 /
  17. AliOS Things学习笔记(5)——ESP8266对接天猫精灵下篇(设备端)
  18. 2016年上半年信息系统监理师考试感想
  19. 6种自动化测试框架(总有一款适合你)
  20. Sniffer报文放送

热门文章

  1. 适用场景全新升级!扩展 Dragonfly2 作为分布式缓存系统架构
  2. 51单片机(十七)—— 定时器2寄存器介绍及功能描述
  3. 10.01-runloop
  4. DevOps基础-4.2-基础架构自动化:Golden Image to Foil Ball
  5. 点阵字体(也叫位图字体或光栅字体)、矢量字体
  6. OpenBlas编译方法
  7. 我坐上了扭曲时空的春运火车?!
  8. QUIC 协议初探 - iOS 实践
  9. 排查解决腾讯云服务器存在对外攻击行为,已阻断该服务器对其他服务器端口(TCP:6379)的访问
  10. 整体浴室装修技巧有哪些?