python核心编程 第二版 第五章 习题

初学python,将自己练习的代码发在这存着,若有人看到,希望不吝赐教。~

-----------------------------------------------------------------------------------------------------------------------

5-2:

def product(a, b):
	print 'The product of a & b is %s ', (a * b)

a = float(raw_input('a is '))
b = float(raw_input('b is '))

print product(a, b)

5-3:

def grade(score):
	print '''
		
		A: 90~100
		B: 80~89
		C: 70~79
		D: 60~69
		F: <60
		
		'''
	
	if 90 <= score <= 100:
		return "The Grade is A"
		
	elif 80 <= score <= 89:
		return "The Grade is B"
		
	elif 70 <= score <= 79:
		return "The Grade is C"
		
	elif 60 <= score <= 69:
		return "The Grade is D"
		
	elif 0 <= score < 60:
		return "The Grade is F"
	
	else:
		return "Pleas print the number in scope!"

score = float(raw_input('The score is '))		
print grade(score)

5-4:

print 'Which year you want to check?'

year = int(raw_input('Input the year >>'))

def isleapyr(year):
	if year % 4 != 0:
		return "This year isn't leap year!"
	
	elif year % 4 == 0:
		if year % 400 == 0:
			return "This year is leap year."
		
		elif year % 400 != 0 and year % 100 == 0:
			return "Sorry, but this isn't leap year."
		
		else:
			return "This is leap year."
		
		
			
		
print isleapyr(year)

5-5:

print "You want to exchange some coin?"
print "I have four kind of coins."
print '1 cent, 5 cents, 10 cents and 25 cents.'
print "How much do you want?"

def change(input):
	while True:
		amount = float(raw_input('less then 1$ >>'))
		if amount >= 1:
			print "I don't have enough coins to exchange!"
		else:
			break
	
	if 0.00 < amount < 1.00:
		amount = 100 * amount
		
		c25 = amount / 25
		rest25 = amount % 25
		
		c10 = rest25 / 10
		rest10 = rest25 % 10

		c5 = rest10 / 5
		rest5 = rest10 % 5
		
		c1 = rest5 / 1
		
		return "Here are %d 25cents, %d 10cents, %d 5cents and %d 1cents."  % (c25, c10, c5, c1)
		
print change(input)

5-6:

print "I make a calculator."
print "You can input 2 numbers, 1 operator."
print "And I'll give you the answer."



def calculator(input):
	while True:
		input = str(raw_input("Tell me your expression >> "))
		expr = input.split()
		if len(expr) != 3:
			print "Please use the 'blank' to slice the expression."
		else:
			break
		
	a = int(expr[0])
	b = int(expr[2])
	c = expr[1]
			
	if c == '+':
		return a + b
	elif c == '-':
		return a - b
	elif c == '*':
		return a * b
	elif c == '/':
		return a / b
	elif c == '**':
		return a ** b
	
print calculator(input)

5-8a:

def square(a):
	return (a ** 2)
	
def cube(a):
	return a ** 3

while True:
	print '''
			(s)quare
			(c)ube
			(q)uit
			
		  '''
	choice = str(raw_input('Make your choice >> '))
	if choice in 'scq':
		if choice == 's':
			a = int(raw_input('Tell me the length of side >>'))
			print "The answer is ", square(a)
			
		elif choice == 'c':
			a = int(raw_input('Tell me the length of side >>'))
			print "The answer is ", cube(a)
	
		elif choice == 'q':
			print "Thanks for using. Bye!"
			break
	else:
		print 'I don\'t know, try again!'
		

5-8b:

def sphere(r):
	return 3.0 / 4.0 * 3.14 * (r ** 3)
	
def circle(r):
	return 3.14 * (r ** 2)

while True:
	print '''
			(s)phere
			(c)ircle
			(q)uit
			
		  '''
	choice = str(raw_input('Make your choice >> '))
	if choice in 'scq':
		if choice == 's':
			r = int(raw_input('Tell me the radius >>'))
			print "The answer is ", sphere(r)
			
		elif choice == 'c':
			r = int(raw_input('Tell me the radius >>'))
			print "The answer is ", circle(r)
	
		elif choice == 'q':
			print "Thanks for using. Bye!"
			break
	else:
		print 'I don\'t know, try again!'
		

5-9:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值