《笨办法学python第三版》习题26,原错误代码及正确代码

   #import  ex25 
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.poop(0) #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 def sort_sentence(sentence): 21 """Takes in a full sentence and returns the sorted words.""" 22 words = break_words(sentence) 23 return sort_words(words) 24 25 def print_first_and_last(sentence): 26 """Prints the first and last words of the sentence.""" 27 words = break_words(sentence) 28 print_first_word(words) 29 print_last_word(words) 30 31 def print_first_and_last_sorted(sentence): 32 """Sorts the words then prints the first and last one.""" 33 words = sort_sentence(sentence) 34 print_first_word(words) 35 print_last_word(words) 36 37 print "Let's practice everything." 38 print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' 39 40 poem = """ 41 \tThe lovely world 42 with logic so firmly planted 43 cannot discern \n the needs of love 44 nor comprehend passion from intuition 45 and requires an explantion 46 \n\t\twhere there is none. 47 """ 48 49 print "--------------" 50 print poem 51 print "--------------" 52 53 five = 10 - 2 + 3 - 5 54 print "This should be five: %s" % five 55 56 def secret_formula(started): 57 jelly_beans = started * 500 58 jars = jelly_beans \ 1000 #除法是“/” 59 crates = jars / 100 60 return jelly_beans, jars, crates 61 62 start_point = 10000 63 beans, jars, crates == secret_formula(start-point) # jelly_beans = start_point 64 65 print "With a starting point of: %d" % start_point 66 print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates) 67 68 start_point = start_point / 10 69 70 print "We can also do that this way:" 71 print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont # start_point) 72 73 sentence = "All god\tthings come to those who weight." #good things 74 75 words = ex25.break_words(sentence) 76 sorted_words = ex25.sort_words(words) 77 78 print_first_word(words) 79 print_last_word(words) 80 .print_first_word(sorted_words) # 去掉print前面的 . 81 print_last_word(sorted_words) 82 sorted_words = ex25.sort_sentence(sentence) 83 prin sorted_words # print 84 85 print_irst_and_last(sentence) # print print_first_and_last 86 87 print_first_a_last_sorted(senence) #indent print first_a_last_sorted(sentence)

正确代码:

 1 import ex25
 2 def break_words(stuff):
 3     """This function will break up words for us."""
 4     words = stuff.split(' ')
 5     return words
 6 
 7 def sort_words(words):
 8     """Sorts the words."""
 9     return sorted(words)
10 
11 def print_first_word(words):
12     """Prints the first word after popping it off."""
13     word = words.pop(0)
14     print word
15 
16 def print_last_word(words):
17     """Prints the last word after popping it off."""
18     word = words.pop(-1)
19     print word
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 print "Let's practice everything."
39 print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'
40 
41 poem = """
42 \tThe lovely world
43 with logic so firmly planted
44 cannot discern \n the needs of love
45 nor comprehend passion from intuition
46 and requires an explantion
47 \n\t\twhere there is none.
48 """
49 
50 print "--------------"
51 print poem
52 print "--------------"
53 
54 five = 10 - 2 + 3 - 6
55 print "This should be five: %s" % five
56 
57 def secret_formula(started):
58     jelly_beans = started * 500
59     jars = jelly_beans / 1000
60     crates = jars / 100
61     return jelly_beans, jars, crates
62 
63 start_point = 10000
64 jelly_beans, jars, crates = secret_formula(start_point)
65 
66 print "With a starting point of: %d" % start_point
67 print "We'd have %d jeans, %d jars, and %d crates." % (jelly_beans, jars, crates)
68 
69 start_point = start_point / 10
70 
71 print "We can also do that this way:"
72 print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)
73 
74 sentence = "All god things come to those who weight."
75 
76 words = ex25.break_words(sentence)
77 sorted_words = ex25.sort_words(words)
78 
79 print_first_word(words)
80 print_last_word(words)
81 print_first_word(sorted_words)
82 print_last_word(sorted_words)
83 sorted_words = ex25.sort_sentence(sentence)
84 print sorted_words
85 
86 print print_first_and_last(sentence)
87 
88 print print_first_and_last_sorted(sentence)
View Code

 

转载于:https://www.cnblogs.com/rousi/p/4983626.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值