python习题练习(chapater

发布时间:2019-08-19 09:22:29编辑:auto阅读(2012)

     

    #!/usr/bin/env python
    # coding: utf-8
    'for practise in chapater five'


    #定义一个函数,计算并返回两个数的乘机
    def product(a, b):
     return(a * b)

    #根据分数输出同学的评分成绩(A-F)
    def score(i):
     if (i > 90) & (i < 100):
      return('A')
     elif (i > 80) & (i < 89):
      return('B')
     elif (i > 70) & (i < 79):
      return('C')
     elif (i > 60) & (i < 69):
      return('D')
     else:
      return('F')
    #print(score(88))
      
    #-----练习取余-----  

    #1.判断年份是否是闰年
    def estimate_leap(j):  #j 就是年份
     if ((j%4 == 0) and (j%100 != 0)) or (j%400 == 0):
      print("%d is a leap year." %j)
     else:
      print("%d is a not leap year." %j)
     return
    #print(estimate_leap(1997))

    #2.取一个任意小于1美元的金额,然后计算可以换成最少多少枚硬币。硬币有1,5,10,25美分
    def coin(k):   #k 就是小于1美元的金额
     coin25 = int(k/0.25)
     coin10 = int((k-coin25*0.25)/0.1)
     coin5 = int((k-coin25*0.25-coin10*0.1)/0.05)
     coin1 = int((k-coin25*0.25-coin10*0.1)*100)
     return("%.2f cent can change to at least %d coins." %(k, (coin25+coin10+coin5+coin1)))
    #print(coin(0.87))

    #3.1 使用循环和算术运算,求出0-20之间的所有偶数
    def even_num():  #even number
     for i in range(21):
      if i % 2 == 0:
       print (i),
    #even_num()

    #3.2 使用循环和算术运算,求出0-20之间的所有奇数
    def odd_num():  #odd number(uneven number)
     for i in range(21):
      if i % 2 == 1:
       print (i),
    #odd_num()
      
    #3.3 写一个函数,检测一个整数能否被另一个整数整除。先要求用户输入两个数,然后你的函数判断
    #     两者是否有整除关系,根据判断结果分别返回 True 和 False
    def exact_divi(a, b):  #检测整数a能否被整数b整除, b/a
     if b % a == 0:
      print ('%d can be divisible by %d' %(a, b))
      return True
     else:
      print ('%d can\'t be divisible by %d' %(a, b))
      return False
    #a = int(raw_input('First number> '))
    #b = int(raw_input('Second number> '))
    #exact_divi(a, b)


      
    #-----算术-----
    #1.一个计算器程序,两个操作数加一个运算符:N1 运算符 N2.(未完成)
    def calculate(l):  #l 是表达式:N1 运算符 N2
     num1 = float(l.split(' ')[0])
     num2 = float(l.split(' ')[2])
     opert = l.split(' ')[1]
    # return (num1 opert num2)
    #print(calculate('11.2 - 2.9')) #这个题目还没解决

    #2.转换,写一对函数来进行华氏度到摄氏度的转换。转换公式为C = (F - 32) * (5 / 9)
    def centi_fahren(a):  #参数a为华氏度,该函数的返回值为摄氏度。
     return('%.2f' %((a - 32) * (float(5)/9)))

    #print(centi_fahren(100))

    #3.系统限制。写一段脚本确认一下你的Python 所能处理的整数,长整数,浮点数和复数的范围。
    import sys
    def num_limit():
     print('The limit for int: %d %d' %(-sys.maxint-1, sys.maxint))
     print('The limit for float: %e %e' %(sys.float_info[3], sys.float_info[0]))
     print('The limit for long: %s' %(sys.long_info) )
    #num_limit()

    #4.转换。写一个函数把由小时和分钟表示的时间转换为只用分钟表示的时间。
    def minutes(str):
     hours = int(str.split(':')[0])
     minutes = int(str.split(':')[1])
     minutes_all = hours * 60 + minutes
     return minutes_all
    #print(minutes('12:50'))

    #5.银行利息。写一个函数,以定期存款利率为参数, 假定该账户每日计算复利,请计算并返回年回报率。
    #工商银行定期存日款率0.0035%
    def interest_year_return(capital, rate):  #capital 是本金,rate 是日利率
     capital_ori = capital
     i = 1
     while True:
      capital = capital * (1+rate)  # capital = capital * ((1+rate)**365)
      i += 1
      if i == 366:
       break
     return((capital - capital_ori)/capital_ori)
    #print(interest_year_return(10000, 0.000035))

    #6.最大公约数和最小公倍数。请计算两个整数的最大公约数和最小公倍数。
    def max_com_divisor(a, b):  #整数a和b的最大公约数,使用辗转相除法(algorithm of division)
     if a / b == 0:
      a, b = b, a
      algorithm_lst = [a, b]
      i = 0
      while True:
       algorithm_lst.append(algorithm_lst[i] % algorithm_lst[i+1])
       if algorithm_lst[i+2] == 0:
        return algorithm_lst[i+1]
        break
       else:
        i += 1
    #print max_com_divisor(66, 242)
    def least_com_multiple(a, b): #整数a和b的最小公倍数,同样使用辗转相除法(algorithm of division)
     if a / b == 0:
      a, b = b, a
      algorithm_lst = [a, b]
      i = 0
      while True:
       algorithm_lst.append(algorithm_lst[i] % algorithm_lst[i+1])
       if algorithm_lst[i+2] == 0:
        break
       else:
        i += 1
     lcm = (a * b) / algorithm_lst[i+1]
     return(lcm)
    #print least_com_multiple(12, 33)

     

    #7.家庭财务。给定一个初始金额和月开销数, 使用循环,确定剩下的金额和当月的支出数, 包括最后的支出数。
    def Payment():
     Balance = float(raw_input("Enter opening balance: "))
     Month_pay = float(raw_input("Enter monthly payment: "))
     print("Account Remaining")
     print("Pymt#\tPaid\tBalance")
     print("-----\t------\t-------")
     i = 0
     while True:
      print("%d\t%.2f\t%.2f" %(i, (Month_pay*i), Balance - (Month_pay*i)))
      i += 1
      if Balance - (Month_pay*i) <= 0:
       break
    #Payment()

    #8.随机数。熟读随机数模块然后解下面的题:
    #生成一个有 N 个元素的由随机数 n 组成的列表, 其中 N 和 n 的取值范围分别为: (1 <
    #N <= 100), (0 <= n <= 2**31 -1)。然后再随机从这个列表中取 N (1 <= N <= 100)个随机数
    #出来, 对它们排序,然后显示这个子集。
    import random
    def Random_lst():  #子集中有个N个元素
     Subset = []  #子集
     N = random.randrange(1, 101)  #1 < N <= 100
     for i in range(1, N+1):
      Element = random.randrange(0, 2**31) #元素的取值范围0 - (2**31-1)
      Subset.append(Element)
      Subset.sort()
     return(Subset)  #返回子集
    print(Random_lst())

关键字