码迷,mamicode.com
首页 > 其他好文 > 详细

ex26 恭喜你现在可以考试了

时间:2017-11-01 21:47:15      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:考题   enc   pow   tin   orm   res   you   code   utf-8   

本练习中,考题将ex24中的利用函数处理一段文字的操作和ex25当中创建一系列函数然后在powershell当中运行的操作给放到一起,通过运行之后发现其中的错误,并依次修改。

下面是正确的代码,并指出了已经作出改动的地方。

 1 #-*- coding: UTF-8 -*- 
 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)#pop写成了poop
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 
39 print "Let‘s practice everything."
40 print You\‘d need to know \‘bout escapes with \\ that do \n newlines and \t tabs.
41 
42 poem = """
43 \tThe lovely world
44 with logic so firmly planted
45 cannot discern \n the needs of love
46 nor comprehend passion from intuition
47 and requires an explantion
48 \n\t\twhere there is none.
49 """
50 
51 
52 print "--------------"
53 print poem
54 print "--------------"
55 
56 five = 10 - 2 + 3 - 5
57 print "This should be five: %s" % five#我总觉得这里不应该用%s 而是应该是%d,但是似乎%s也能正常运行。
58 
59 def secret_formula(started):
60     jelly_beans = started * 500
61     jars = jelly_beans / 1000#除号写错了
62     crates = jars / 100
63     return jelly_beans, jars, crates
64 
65 
66 start_point = 10000
67 beans, jars, crates = secret_formula(start_point)#"=="的作用是等于,而不是赋值或者定义变量名
68 
69 print "With a starting point of: %d" % start_point
70 print "We‘d have %d beans, %d jars, and %d crates." % (beans, jars, crates)#虽然不影响运行,但是beans确实被写成了jeans
71 
72 start_point = start_point / 10
73 
74 print "We can also do that this way:"
75 print "We‘d have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)#point写错了并且少了一个")"
76 
77 
78 sentence = "All god\tthings come to those who weight."
79 
80 import ex25 #要想使用下面的函数,必须要引入一个ex25,因为里边包含了这些函数
81 words = ex25.break_words(sentence)
82 sorted_words = ex25.sort_words(words)
83 
84 print_first_word(words)
85 print_last_word(words)
86 print_first_word(sorted_words)#前面多加了一个"."
87 print_last_word(sorted_words)
88 sorted_words = ex25.sort_sentence(sentence)
89 print sorted_words
90 
91 print_first_and_last(sentence)
92 
93 print_first_and_last_sorted(sentence)#这里多打了一个tab,并且写错了函数名中的“and";写错了sentence

 

ex26 恭喜你现在可以考试了

标签:考题   enc   pow   tin   orm   res   you   code   utf-8   

原文地址:http://www.cnblogs.com/dingtou00/p/7768281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!