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

继续之前的练习

本章首先介绍了如何编写和调用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=0
    for 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+=x
    print("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/112
    else:
        ans=S//112+1
    print("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 None
    if a>b:
        return a
    else:
        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 True
    if n<2:
        return False
    if n%2==0:
        return False
    i=3
    while i*i<=n:
        if n%i==0:
            return False
        i+=2
    return 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 True
    if n<2:
        return False
    if n%2==0:
        return False
    i=3
    while i*i<=n:
        if n%i==0:
            return False
        i+=2
    return True
def main():
    for i in range(1,101):
        x=i
        if 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/=100
    t=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:
            break
        x = 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 True
    else:
        return False
def main():
    #1-stone 2-cloth 3-scissors
    y=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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值