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:

你可能感兴趣的:(python,初学python)