习题 26: 恭喜你,现在可以考试了!
你已经差不多完成这本书的前半部分了,不过后半部分才是更有趣的。你将学到逻辑,并通过条件判断实现有用的功能。
在你继续学习之前,你有一道试题要做。这道试题很难,因为它需要你修正别人写的代码。当你成为程序员以后,你将需要经常面对别的程序员的代码,也许还有他们的傲慢态度,他们会经常说自己的代码是完美的。
这样的程序员是自以为是不在乎别人的蠢货。优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能,他们会先假设是自己的代码有问题,然后用排除法清查所有可能是自己有问题的地方,最后才会得出“这是别人的错误”这样的结论。
在这节练习中,你将面对一个水平糟糕的程序员,并改好他的代码。我将习题 24 和 25 胡乱拷贝到了一个文件中,随机地删掉了一些字符,然后添加了一些错误进去。大部分的错误是Python 在执行时会告诉你的,还有一些算术错误是你要自己找出来的。再剩下来的就是格式和拼写错误了。
所有这些错误都是程序员很容易犯的,就算有经验的程序员也不例外。
你的任务是将此文件修改正确,用你所有的技能改进这个脚本。你可以先分析这个文件,或者你还可以把它像学期论文一样打印出来,修正里边的每一个缺陷,重复修正和运行的动作,直到这个脚本可以完美地运行起来。在整个过程中不要寻求帮助,如果你卡在某个地方无法进行下去,那就休息一会晚点再做。
就算你需要几天才能完成,也不要放弃,直到完全改对为止。
最后要说的是,这个练习的目的不是写程序,而是修正现有的程序,你需要访问下面的网站:
从那里把代码复制粘贴过来,命名为 ex26.py,这也是本书唯一一处允许你复制粘贴的地方。
改完后如图所示:
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 def print_first_word(words): 13 """Prints the first word after popping it off.""" 14 # word = words.poop(0) 15 word = words.pop(0) 16 print word 17 18 def print_last_word(words): 19 """Prints the last word after popping it off.""" 20 # word = words.pop(-1 21 word = words.pop(-1) 22 print word 23 24 def sort_sentence(sentence): 25 """Takes in a full sentence and returns the sorted words.""" 26 words = break_words(sentence) 27 return sort_words(words) 28 29 def print_first_and_last(sentence): 30 """Prints the first and last words of the sentence.""" 31 words = break_words(sentence) 32 print_first_word(words) 33 print_last_word(words) 34 35 def print_first_and_last_sorted(sentence): 36 """Sorts the words then prints the first and last one.""" 37 words = sort_sentence(sentence) 38 print_first_word(words) 39 print_last_word(words) 40 41 42 print "Let's practice everything." 43 print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' 44 45 poem = """ 46 \tThe lovely world 47 with logic so firmly planted 48 cannot discern \n the needs of love 49 nor comprehend passion from intuition 50 and requires an explantion 51 \n\t\twhere there is none. 52 """ 53 54 55 print "--------------" 56 print poem 57 print "--------------" 58 59 five = 10 - 2 + 3 - 5 60 print "This should be five: %s" % five 61 62 def secret_formula(started): 63 jelly_beans = started * 500 64 # jars = jelly_beans \ 1000 65 jars = jelly_beans / 1000 66 crates = jars / 100 67 return jelly_beans, jars, crates 68 69 70 start_point = 10000 71 # beans, jars, crates == secret_formula(start-point) 72 beans, jars, crates = secret_formula(start_point) 73 74 print "With a starting point of: %d" % start_point 75 print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates) 76 77 start_point = start_point / 10 78 79 print "We can also do that this way:" 80 # print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont 81 print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point) 82 83 84 sentence = "All god\tthings come to those who weight." 85 86 words = ex25.break_words(sentence) 87 sorted_words = ex25.sort_words(words) 88 89 print_first_word(words) 90 print_last_word(words) 91 # .print_first_word(sorted_words) 92 print_first_word(sorted_words) 93 print_last_word(sorted_words) 94 sorted_words = ex25.sort_sentence(sentence) 95 # prin sorted_words 96 print sort_words(words) 97 98 # print_irst_and_last(sentence) 99 print_first_and_last(sentence) 100 101 # print_first_a_last_sorted(senence) 102 print_first_and_last_sorted(sentence)