你已经差不多完成这本书的前半部分了,不过后半部分才是更有趣的。你将学到逻辑,并通过条件判断实现有用的功能。

在你继续学习之前,你有一道试题要做。这道试题很难,因为它需要你修正别人写的代码。当你成为程序员以后,你将需要经常面对别的程序员的代码,也许还有他们的傲慢态度,他们会经常说自己的代码是完美的。

这样的程序员是自以为是不在乎别人的蠢货。优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能,他们会先假设是自己的代码有问题,然后用排除法清查所有可能是自己有问题的地方,最后才会得出“这是别人的错误”这样的结论。

在这节练习中,你将面对一个水平糟糕的程序员,并改好他的代码。我将习题 24 和 25 胡乱拷贝到了一个文件中,随机地删掉了一些字符,然后添加了一些错误进去。大部分的错误是 Python 在执行时会告诉你的,还有一些算术错误是你要自己找出来的。再剩下来的就是格式和拼写错误了。

所有这些错误都是程序员很容易犯的,就算有经验的程序员也不例外。

你的任务是将此文件修改正确,用你所有的技能改进这个脚本。你可以先分析这个文件,或者你还可以把它像学期论文一样打印出来,修正里边的每一个缺陷,重复修正和运行的动作,直到这个脚本可以完美地运行起来。在整个过程中不要寻求帮助,如果你卡在某个地方无法进行下去,那就休息一会晚点再做。

就算你需要几天才能完成,也不要放弃,直到完全改对为止。

最后要说的是,这个练习的目的不是写程序,而是修正现有的程序,你需要访问下面的网站:

�6�1 http://learnpythonthehardway.org/book/exercise26.txt

从那里把代码复制粘贴过来,命名为 ex26.py ,这也是本书唯一一处允许你复制粘贴的地方。

常见问题回答

一定要 import ex25.py 吗?移除对它的引用也可以吧?

怎样都可以。不过这个文件里会用到 ex25 中的函数,你可以试着移除引用看看会怎样。

我可以边修正代码边运行吗?

当然可以。这样的事情就是要计算机帮忙,多多益善。

试题内容如下: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  newlines and \t tabs.'

poem = """

\tThe lovely world

with logic so firmly planted

cannot discern  the needs of love

nor comprehend passion from intuition

and requires an explantion

\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)

修改后# -*- coding:utf-8 -*-

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)    # 取第一个字母 单词拼写错误

return word # print 更改 return,否则会返回none

def print_last_word(words):

"""Prints the last word after popping it off."""

word = words.pop(-1)    # 取最后一个字母

return word # print 更改 return,否则会返回none

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)

return print_first_word(words)

return 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)

return print_first_word(words)

return print_last_word(words)

print "Let's practice everything."

print 'You\'d need to know \'bout escapes with \\ that do  newlines and \t tabs.'

poem = """

\tThe lovely world

with logic so firmly planted

cannot discern  the needs of love

nor comprehend passion from intuition

and requires an explantion

\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_point)  #少了一个) start_point平措

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

words = break_words(sentence)

sorted_words = sort_words(words)

print_first_word(words)

print_last_word(words)

print_first_word(sorted_words) #print都了一个.

print_last_word(sorted_words)

sorted_words = sort_sentence(sentence)

print sorted_words

print_first_and_last(sentence)  #单词拼写错误

print_first_and_last_sorted(sentence)    #单词拼写错误

运行结果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', 'come', 'god\tthings', 'those', 'to', 'weight.', 'who']

python考试代码复制_笨办法学Python 习题 26: 恭喜你,现在可以考试了! 错误代码下载链接...相关推荐

  1. 笨办法学python第五版_笨办法学python PDF下载|笨办法学python第五版 电子版附目录_最火软件站...

    笨办法学Python第五版PDF电子版是专门为初学Python的朋友准备的一款电子图书资料,可以帮助你更好的学习Python编程知识,该电子书每章节都附带了常见问题回答以及练习题目,能够帮助学习者快速 ...

  2. python描述器 有限状态机_笨办法学 Python · 续 练习 30:有限状态机

    练习 30:有限状态机 每当你阅读一本关于解析的书,都有一个可怕的章节,关于有限状态机(FSM).他们对"边"和"节点"进行了详细的分析,每个可能的" ...

  3. 笨办法学python第五版_笨办法学Python(五)

    习题 5: 更多的变量和打印 我们现在要键入更多的变量并且把它们打印出来.这次我们将使用一个叫"格式化字符串(format string)"的东西. 每一次你使用 " 把 ...

  4. 笨方法学python第六版_笨办法学Python(六)

    习题 6: 字符串(string)和文本 虽然你已经在程序中写过字符串了,你还没学过它们的用处.在这章习题中我们将使用复杂的字符串来建立一系列的变量,从中你将学到它们的用途.首先我们解释一下字符串是什 ...

  5. 笨办法学python pdf 第三版_笨办法学python第三版

    笨办法学python第三版pdf电子书是一本Python学习参考书,是美国程序员Zed A.Shaw编著,通过简单通俗的方法,结合内部的集体,让程序员学懂python,适用于初级学习python编程的 ...

  6. 笨办法学python在线阅读_笨办法学python全集.pdf

    TableofContents 笨办法学Python 1.1 序言 1.2 前言 1.3 简介 1.4 练习0.安装和准备 1.5 练习1.第一个程序 1.6 练习2.注释和井号"#&quo ...

  7. 笨办法学python3进阶篇下载_笨办法学Python 3 进阶篇

    书名:"笨办法"学Python 3:进阶篇 定价:59.0 ISBN:9787115505392 作者: 泽德·A. 肖 版次:第1版 出版时间:2020-06 内容提要: 本书是 ...

  8. 笨方法学 python3进阶篇_笨办法学Python 3 进阶篇

    部分准备知识1 如果不喜欢作者的个人流程怎么办2 如果发现自己太糟糕怎么办2 习题0准备工作3 程序员用的编辑器3 Python3.63 工作终端4 pip和virtualenv的配置4 实验笔记4 ...

  9. 笨办法学python3进阶篇_笨办法学Python 3 进阶篇

    第 一部分 准备知识 1 如果不喜欢作者的个人流程怎么办 2 如果发现自己太糟糕怎么办 2 习题0 准备工作 3 程序员用的编辑器 3 Python 3.6 3 工作终端 4 pip和virtuale ...

最新文章

  1. log4j debug写法
  2. PythonOpenCV--Rtrees随机森林
  3. server 2008R2 AD域环境中DHCP服务器的授权步骤
  4. Android | Sqlite3
  5. python 修饰器 参数_具有参数的Python类修饰器,但未运行修饰的...
  6. java计算一个多边形的重心_2D凸多边形碰撞检测算法(二) - GJK(上)
  7. 拼多多联合三奇医卫等企业,每天上架1000万只平价口罩
  8. Apache Flink 进阶(一):Runtime 核心机制剖析
  9. topcoder srm 440 div1
  10. 家里还装摄像头?看完B站直播毛骨悚然!
  11. 高等数学(第七版)同济大学 习题1-5 个人解答
  12. 球体重量在线计算机,用图形计算器研究热气球.doc
  13. Typora中写论文怎么添加reference(参考文献)
  14. Shell脚本到底是什么高大上的技术吗?
  15. 【数据分析】基于RFM模型的线上零售中的客户细分(一):客户细分
  16. CS224n笔记——Subword Model(十二)
  17. 用知识图谱打开梁山好汉一百单八将
  18. java算法int型整数反转的另类解法
  19. Java字符拼成图片
  20. 超级码力在线编程大赛初赛第1场-1-树木规划题解

热门文章

  1. c# 文章分享微信朋友圈自定义标题、摘要、缩略图
  2. codevs3287货车运输[倍增]
  3. 咖说 | TON 项目宣告终止!但社区还将继续进行开发和发币
  4. 微信转化接口(2018-06-14)
  5. 【esp8266】①烧录指南
  6. Relation (mathematics)
  7. HDOJ2036改革春风吹满地笔记——任意多边形求面积
  8. access mysql odbc驱动程序_64位系统下access的64位odbc驱动
  9. 微信小程序--订阅消息推送
  10. 实现登录与注册界面(1号店)