首页 > 其他 > 详细

ex26

时间:2017-02-01 11:43:06      阅读:361      评论:0      收藏:0      [点我收藏+]



以下就原代码(有错误的已经加粗显示)

 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)
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 
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 
51 print "--------------"
52 print poem
53 print "--------------"
54 
55 five = 10 - 2 + 3 - 5
56 print "This should be five: %s" % five
57 
58 def secret_formula(started):
59     jelly_beans = started * 500
60     jars = jelly_beans \ 1000
61     crates = jars / 100
62     return jelly_beans, jars, crates
63 
64 
65 start_point = 10000
66 beans, jars, crates == secret_formula(start-point)
67 
68 print "With a starting point of: %d" % start_point
69 print "We‘d have %d jeans, %d jars, and %d crates." % (beans, jars, crates)
70 
71 start_point = start_point / 10
72 
73 print "We can also do that this way:"
74 print "We‘d have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
75 
76 
77 sentence = "All god\tthings come to those who weight."
78 
79 words = ex25.break_words(sentence)
80 sorted_words = ex25.sort_words(words)
81 
82 print_first_word(words)
83 print_last_word(words)
84 .print_first_word(sorted_words)
85 print_last_word(sorted_words)
86 sorted_words = ex25.sort_sentence(sentence)
87 prin sorted_words
88 
89 print_irst_and_last(sentence)
90 
91    print_first_a_last_sorted(senence)

总结:本章不难,只要认真打字跟分析过EX24,25两章基本没问题。都是一些语法错误,漏打,错打以及KEYWORD打错等问题。

值得留意:行79开始ex25.要去掉,因为sentence在行77已经有加入,无需像EX25一样要用import赋值

 

正确源代码:

 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 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 
38 print "Let‘s practice everything."
39 print You\‘d need to know \‘bout escapes with \\ that do \nnewlines and \ttabs.
40 
41 poem = """
42 \tThe lovely world
43 with logic so firmly planted
44 cannot discern \nthe needs of love
45 nor comprehend passion from intuition
46 and requires an explantion
47 \n\t\twhere there is none.
48 """
49 
50 
51 print "--------------"
52 print poem
53 print "--------------"
54 
55 five = 10 - 2 + 3 - 5
56 print "This should be five: %s" % five
57 
58 def secret_formula(started):
59     jelly_beans = started * 500
60     jars = jelly_beans / 1000
61     crates = jars / 100
62     return jelly_beans, jars, crates
63 
64 
65 start_point = 10000
66 beans, jars, crates = secret_formula(start_point)
67 
68 print "With a starting point of: %d" % (start_point)
69 print "We‘d have %d jeans, %d jars, and %d crates." % (beans, jars, crates)
70 
71 start_pointed = start_point / 10
72 
73 print "We can also do that this way:"
74 print "We‘d have %d beans, %d jars, and %d crabapples." % secret_formula(start_pointed)
75 
76 sentence = "All god things come to those who weight."
77 
78 words = break_words(sentence)
79 sorted_words = sort_words(words)
80 
81 print_first_word(words)
82 print_last_word(words)
83 print_first_word(sorted_words)
84 print_last_word(sorted_words)
85 sorted_words = sort_sentence(sentence)
86 print sorted_words
87 
88 print_first_and_last(sentence)
89 print_first_and_last_sorted(sentence)

 

运行结果:

技术分享

 

ex26

原文:http://www.cnblogs.com/LevenLau/p/6359842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!