笨办法学Python(learn python the hard way)--练习程序21-30

下面是练习21-30,基于python3

 #ex21.py
1
def add(a, b): 2 print("ADDING %d + %d" %(a, b)) 3 return a+b 4 5 def subtract(a, b): 6 print("SUBTRACTING %d - %d" %(a, b)) 7 return a-b 8 9 def multiply(a, b): 10 print("MULTIPLYING %d * %d" %(a, b)) 11 return a*b 12 13 def divide(a, b): 14 print("DIVIDING %d / %d" %(a, b)) 15 return a / b 16 17 18 print("Let's do some math with just functions!") 19 20 age = add(30, 5) 21 height = subtract(78, 4) 22 weight = multiply(90, 2) 23 iq = divide(100, 2) 24 25 print("Age: %d, Height: %d, Weight: %d, IQ: %d"%(age,height,weight,iq)) 26 27 28 # A puzzle for the extra credit, type it in anyway. 29 print("Here is a puzzle.") 30 31 what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) 32 33 print("That becomes: ", what, "Can you do it by hand?")

 

 #ex24.py
1
print("Let's practice everything.") 2 print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') 3 4 5 poem = """ 6 \tThe lovely world 7 with logic so firmly planted 8 cannot discern \n the needs of love 9 nor comprehend passion from intuition 10 and requires an explanation 11 \n\t\twhere there is none. 12 """ 13 14 print("--------------") 15 print(poem) 16 print("--------------") 17 18 19 five = 10 - 2 + 3 - 6 20 print("This should be five: %s" % five) 21 22 def secret_formula(started): 23 jelly_beans = started * 500 24 jars = jelly_beans / 1000 25 crates = jars / 100 26 return jelly_beans, jars, crates 27 28 29 start_point = 10000 30 beans, jars, crates = secret_formula(start_point) 31 32 print("With a starting point of: %d" % start_point) 33 print("We'd have %d beans, %d jars, and %d crates."%(beans, jars, crates)) 34 35 36 start_point = start_point / 10 37 38 print("We can also do that this way:") 39 print("We'd have %d beans, %d jars, and %d crates." %secret_formula(start_point))

 

 #ex25.py
1
def break_words(stuff): 2 """This function will break up words for us.""" 3 words = stuff.split(' ') 4 return words 5 6 def sort_words(words): 7 """Sorts the words.""" 8 return sorted(words) 9 10 def print_first_word(words): 11 """Prints the first word after popping it off.""" 12 word = words.pop(0) 13 print(word) 14 15 def print_last_word(words): 16 """Prints the last word after popping it off.""" 17 word = words.pop(-1) 18 print(word) 19 20 21 def sort_sentence(sentence): 22 """Takes in a full sentence and returns the sorted words.""" 23 words = break_words(sentence) 24 return sort_words(words) 25 26 def print_first_and_last(sentence): 27 """Prints the first and last words of the sentence.""" 28 words = break_words(sentence) 29 print_first_word(words) 30 print_last_word(words) 31 32 def print_first_and_last_sorted(sentence): 33 """Sorts the words then prints the first and last one.""" 34 words = sort_sentence(sentence) 35 print_first_word(words) 36 print_last_word(words) 37 38 sentence = "All good things come to those who wait"

 

 #ex29.py
1
people = 20 2 cats = 30 3 dogs = 15 4 5 6 if people < cats: 7 print("Too many cats! The world is doomed!") 8 9 if people > cats: 10 print("Not many cats! The world is saved!") 11 12 if people < dogs: 13 print("The world is drooled on!") 14 15 if people > dogs: 16 print("The world is dry!") 17 18 19 dogs += 5 20 21 if people >= dogs: 22 print("People are greater than or equal to dogs.") 23 24 if people <= dogs: 25 print("People are less than or equal to dogs.") 26 27 28 if people == dogs: 29 print("People are dogs.")

 

 #ex30.py
1
people = 30 2 cars = 40 3 buses = 15 4 5 6 if cars > people: 7 print("We should take the cars.") 8 elif cars < people: 9 print("We should not take the cars.") 10 else: 11 print("We can't decide.") 12 13 if buses > cars: 14 print("That's too many buses.") 15 elif buses < cars: 16 print("Maybe we could take the buses.") 17 else: 18 print("We still can't decide.") 19 20 if people > buses: 21 print("Alright, let's just take the buses.") 22 else: 23 print("Fine, let's stay home then.")

 

转载于:https://www.cnblogs.com/xiyouzhi/p/9600456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值