你已经差不多完成这本书的前半部分了,不过后半部分才是更有趣的。你将学到逻辑,并通过条件判断实现有用的功能。
在你继续学习之前,你有一道试题要做。这道试题很难,因为它需要你修正别人写的代码。当你成为程序员以后,你将需要经常面对别的程序员的代码,也许还有他们的傲慢态度,他们会经常说自己的代码是完美的。
这样的程序员是自以为是不在乎别人的蠢货。优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能,他们会先假设是自己的代码有问题,然后用排除法清查所有可能是自己有问题的地方,最后才会得出“这是别人的错误”这样的结论。
在这节练习中,你将面对一个水平糟糕的程序员,并改好他的代码。我将习题 24 和 25 胡乱拷贝到了一个文件中,随机地删掉了一些字符,然后添加了一些错误进去。大部分的错误是Python 在执行时会告诉你的,还有一些算术错误是你要自己找出来的。再剩下来的就是格式和拼写错误了。
所有这些错误都是程序员很容易犯的,就算有经验的程序员也不例外。
你的任务是将此文件修改正确,用你所有的技能改进这个脚本。你可以先分析这个文件,或者你还可以把它像学期论文一样打印出来,修正里边的每一个缺陷,重复修正和运行的动作,直到这个脚本可以完美地运行起来。在整个过程中不要寻求帮助,如果你卡在某个地方无法进行下去,那就休息一会晚点再做。
就算你需要几天才能完成,也不要放弃,直到完全改对为止。
最后要说的是,这个练习的目的不是写程序,而是修正现有的程序,你需要访问下面的网站:
http://learnpythonthehardway.org/book/exercise26.txt
从那里把代码复制粘贴过来,命名为 ex26.py,这也是本书唯一一处允许你复制粘贴的地方。
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.poop(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: %s" % 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_pont
sentence = "All god\tthings come to those who weight."
words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)
print_first_word(words)
print_last_word(words)
.print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
prin sorted_words
print_irst_and_last(sentence)
print_first_a_last_sorted(senence)
第一版ex26-1.py,看代码修改,修改并解释:
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.poop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
#word = words.pop(-1 # here lost a ")"
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: %s" % five # change %s to %d
print " This should be five: %d" % five
def secret_formula(started):
jelly_beans = started * 500
# jars = jelly_beans \ 1000 #change the back-slash to slash
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
# beans, jars, crates == secret_formula(start-point) # change "==" to "="
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_pont # here lost a ")"
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 = ex25.break_words(sentence) # in a same file, delelte "ex25."
words = break_words(sentence)
#sorted_words = ex25.sort_words(words)# in a same file, delelte "ex25."
sorted_words = sort_words(words)
print_first_word(words)
print_last_word(words)
# .print_first_word(sorted_words) # delete the "."
print_first_word(sorted_words)
print_last_word(sorted_words)
#sorted_words = ex25.sort_sentence(sentence)# in a same file, delelte "ex25."
sorted_words = sort_sentence(sentence)
# prin sorted_words # prin is incorrect
print sorted_words
#print_irst_and_last(sentence) #irst is incorrect,print_irst_and_last is not defined
print_first_and_last(sentence)
# print_first_a_last_sorted(senence)#1st,the/tab is incorrect;2nd,a is incorrect,print_first_a_last_sorted is not defined;3rd,senence is not defined
print_first_and_last_sorted(sentence)
运行该python程序,出错:
$python ex26-1.py
File "ex26-1.py", line 10
def print_first_word(words)
^
SyntaxError: invalid syntax
$
创建函数的规则没有记清楚,第六条,紧跟着参数的是不是括号和冒号? 这里少了冒号,第二版修改,加上冒号。
def print_first_word(words):
运行:
$python ex26-2.py
Let's practice everything.
You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion
where there is none.
--------------
This should be five: 6
Traceback (most recent call last):
File "ex26-1.py", line 71, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined
$
修改71行:
beans, jars, crates = secret_formula(start-point) 原来脚本里的start-point没有定义。
我们可以看第69行,定义的是start_point;第三版,这里将start-point改为start_point
运行:
$python ex26-3.py
Let's practice everything.
You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion
where there is none.
--------------
This should be five: 6
With a starting point of: 10000
We'd have 5000000 jeans, 5000 jars, and 50 crates.
We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crabapples.
Traceback (most recent call last):
File "ex26-3.py", line 91, in <module>
print_first_word(words)
File "ex26-3.py", line 13, in print_first_word
word = words.poop(0)
AttributeError: 'list' object has no attribute 'poop'
$
提示list对象没有poop属性
拼写错误,第四版,将poop改为pop
word = words.pop(0)
运行该程序,成功了:
$python ex26-4.py
Let's practice everything.
You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion
where there is none.
--------------
This should be five: 6
With a starting point of: 10000
We'd have 5000000 jeans, 5000 jars, and 50 crates.
We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crabapples.
All
weight.
All
who
['All', 'come', 'god\tthings', 'those', 'to', 'weight.', 'who']
All
weight.
All
who
$
但是我们看结果里面有一些单词拼写或者其他小错误,比如:
“This should be five: 6” 这句话内容相悖啊
jeans这个单词应该是beans啊
god\tthings应该是good things啊
这些就先不管了。
将改完后的Python程序ex26-4.py贴上来:
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) # here lost a ":" == ver02
def print_first_word(words):
"""Prints the first word after popping it off."""
# word = words.poop(0) # 'list' object has no attribute 'poop',change to "pop" == ver04
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
#word = words.pop(-1 # here lost a ")" == ver01
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: %s" % five # change %s to %d == ver01
print " This should be five: %d" % five
def secret_formula(started):
jelly_beans = started * 500
# jars = jelly_beans \ 1000 # change the back-slash to slash == ver01
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
# beans, jars, crates == secret_formula(start-point) # change "==" to "=" == ver01
# beans, jars, crates = secret_formula(start-point)# change to "start_point" == ver03
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_pont
# here lost a ")" == ver01
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 = ex25.break_words(sentence) # in a same file, delelte "ex25." == ver01
words = break_words(sentence)
# sorted_words = ex25.sort_words(words)# in a same file, delelte "ex25." == ver01
sorted_words = sort_words(words)
print_first_word(words)
print_last_word(words)
# .print_first_word(sorted_words) # delete the "." == ver01
print_first_word(sorted_words)
print_last_word(sorted_words)
#sorted_words = ex25.sort_sentence(sentence)# in a same file, delelte "ex25." == ver01
sorted_words = sort_sentence(sentence)
# prin sorted_words # prin is incorrect == ver01
print sorted_words
#print_irst_and_last(sentence) #irst is incorrect,print_irst_and_last is not defined == ver01
print_first_and_last(sentence)
# print_first_a_last_sorted(senence)
#1st,the/tab is incorrect;
#2nd,a is incorrect,print_first_a_last_sorted is not defined;
#3rd,senence is not defined == ver01
print_first_and_last_sorted(sentence)
至此,笨方法学python-ex26-恭喜你,现在可以考试了!这一小节的学习结束了。