python核心编程第五章练习

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 * 100
		cents_25 = 0
		cents_5 = 0
		cents_1 = 0
		if 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 = cents
		elif cents >= 5:
			cents_5 = divmod(cents, 5)[0]
			cents_1 = divmod(cents, 5)[1]
		else:
			cents_1 = cents
		return "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_arr

print get_event_and_odd(20)

5-12

import sys 

print sys.maxint
print -sys.maxint-1
print sys.float_info
print 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 = 0
	while balance > paid:
		print '%s\t$%s\t$%s' % (i,paid,balance)
		balance = balance - paid
		balance = round(balance, 2)
		i += 1
	print '%s\t$%s\t$0.00' % (i,balance)

payment()
5-17

import random

arr = []

def rand_sort_num():
	N = random.randint(1, 100)
	while N > 0:
		n = random.randint(0, 2**31 -1)
		arr.append(n)
		N = N - 1

	arr.sort()
	return arr

print rand_sort_num()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值