您的位置:首页 > 编程语言 > Python开发

Exercise 26:恭喜你,现在可以参加考试了

2014-03-26 21:12 330 查看
原文链接:http://learnpythonthehardway.org/book/ex26.html

       你现在已经学习了这本书几乎一半内容了。剩下的这一半内容将更加有趣。你将学到逻辑,并通过条件判断来实现有用的功能。

       在你继续学习之前,我们先要对你做一次考验。这个考验将会非常的难因为它要求你修复别人的代码。 当你成为一个程序员的时候你常常需要去处理别的程序员的代码并且时常也要忍受它们的傲慢态度。程序员时常宣称他们的代码时完美的。

       这种自以为是不关心别人的程序员是愚蠢的。一个优秀的程序员会像一个优秀的科学家那样承认他们的代码总会有一定的几率会出错。优秀的程序员在确认可能是由别人的代码引起的错误之前,会先从发生错误的地方开始逐步排查自己的代码中可能引起错误的地方。

       在这次的练习中,你将面对一个水平糟糕的程序员,并改好他的代码。我将Exercise 24 和 25胡乱的拷贝到一个文件里,然后随机的移除了一些字符和 添加了一些错误进去。大多数的错误Python是会告诉你的,但是一些数学计算的错误需要你自己去找。以及在字符串中格式化错误或者拼写错误也需要注意。

       所有的错误都是所有程序甚至有经验的程序员经常会犯的错误。

       你这次练习的任务就是将这个文件修改正确。使用你会的所有技巧让这个文件更加完善。先分析脚本,或者你可以像打印学期论文那样将其打印出来。修复一个错误就运行一次,然后接着修复再运行如此操作直达脚本完美运行。不要试图去寻求帮助,如果遇到你无法解决的你可以先将它放一放回过头再来解决它。

       即使这个可能要花费你几天时间去做,但是你也得努力通过这次考验把这个脚本文件修改正确。

       最后,这次提到的这个练习不是自己输入而是修复一个已经有的脚本文件。那样做的话,你必须到厦门这个地址去下载相关文件:http://learnpythonthehardway.org/book/exercise26.txt

       把这个文本中的内容复制黏贴到命名为ex26.py的文件中。仅仅这一次你允许去复制黏贴操作。

学生遇见的常见问题:

我应该将ex25.py文件作为模块引入还是就简单移除ex25的引用操作?
答:两种方法都可以。这个文件中的函数既然就是来自ex25 ,那么你就先把它的引用去掉。

当我们修复了该文件是否能够运行这个脚本文件?
答:肯定可以啊。电脑就在手边,所以尽可能的使用电脑来帮助自己解决。

ps:附上自己修复好的文件:
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words

def sort_words(words):
"""Sorts the words."""
return sorted(words)

def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word

def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)

print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""

print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
print "This should be five: %d" % five

def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates

start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)

sentence = "All god\tthings come to those who weight."

words = break_words(sentence)
sorted_words = sort_words(words)

print words
print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
print sorted_words

print_first_and_last(sentence)

print_first_and_last_sorted(sentence)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python test 脚本
相关文章推荐