python核心编程第五章练习

来源:互联网 发布:知彼定位 编辑:程序博客网 时间:2024/04/28 12:06

5-3.Standard Type Operators。

def get_letter_grade(score):if score >0 and score <100:if score >=90:return 'A'elif score >=80:return 'B'elif score >=70:return 'C'elif score >=60:return 'D'else:return 'F'else:return 'Error input'

5-4

def isleapyear(year):    if (year %4 == 0 and year %100 !=0) or (year %400 == 0 ):        return True    else:        return False

5-5

def get_cent_count(num):if num <= 1:cents = num * 100cents_25 = 0cents_5 = 0cents_1 = 0if cents >= 25:cents_25 = divmod(cents, 25)[0]cents = divmod(cents, 25)[1]if cents >= 5:cents_5 = divmod(cents, 5)[0]cents_1 = divmod(cents, 5)[1]else:cents_1 = centselif cents >= 5:cents_5 = divmod(cents, 5)[0]cents_1 = divmod(cents, 5)[1]else:cents_1 = centsreturn "25 cents count %d, 5 cents count %d, 1 cents count %d."%(cents_25,cents_5,cents_1)else:print "Error input"
5-11

def get_event_and_odd(num):event_arr = []odd_arr = []for i in xrange(num+1):if i % 2 == 0:event_arr.append(i)else:odd_arr.append(i)return event_arr, odd_arrprint get_event_and_odd(20)

5-12

import sys print sys.maxintprint -sys.maxint-1print sys.float_infoprint sys.long_info

5-16

def payment():balance = float(raw_input('Enter opening balance: '))paid = float(raw_input('Enter monthly payment:'))balance = round(balance, 2)paid = round(paid, 2)print '\tAmount\tRemaining'print 'Pymt#\tPaid\tBalance'print '-----\t-----\t-----'i = 0while balance > paid:print '%s\t$%s\t$%s' % (i,paid,balance)balance = balance - paidbalance = round(balance, 2)i += 1print '%s\t$%s\t$0.00' % (i,balance)payment()
5-17

import randomarr = []def rand_sort_num():N = random.randint(1, 100)while N > 0:n = random.randint(0, 2**31 -1)arr.append(n)N = N - 1arr.sort()return arrprint rand_sort_num()


0 0
原创粉丝点击