习题 26: 恭喜你,现在可以考试了!

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

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

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

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

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

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

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

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

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

改完后如图所示:

 import 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)
def print_first_word(words):
"""Prints the first word after popping it off."""
# word = words.poop(0)
word = words.pop(0)
print word def print_last_word(words):
"""Prints the last word after popping it off."""
# word = words.pop(-1
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
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates start_point = 10000
# beans, jars, crates == secret_formula(start-point)
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
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)
sorted_words = ex25.sort_words(words) print_first_word(words)
print_last_word(words)
# .print_first_word(sorted_words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
# prin sorted_words
print sort_words(words) # print_irst_and_last(sentence)
print_first_and_last(sentence) # print_first_a_last_sorted(senence)
print_first_and_last_sorted(sentence)

笨办法学Python(二十六)的更多相关文章

  1. 笨办法学Python(十六)

    习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...

  2. 笨办法学Python(十二)

    习题 12:提示别人 当你键入 raw_input() 的时候,你需要键入 ( 和 ) 也就是“括号(parenthesis)”.这和你格式化输出两个以上变量时的情况有点类似,比如说 "%s ...

  3. 笨办法学Python(十九)

    习题 19: 函数和变量 函数这个概念也许承载了太多的信息量,不过别担心.只要坚持做这些练习,对照上个练习中的检查点检查一遍这次的联系,你最终会明白这些内容的. 有一个你可能没有注意到的细节,我们现在 ...

  4. 笨办法学Python(十八)

    习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...

  5. 笨办法学Python(十五)

    习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...

  6. 笨办法学Python(十)

    习题 10: 那是什么? 在习题 9 中我你接触了一些新东西.我让你看到两种让字符串扩展到多行的方法.第一种方法是在月份之间用 \n (back-slash n )隔开.这两个字符的作用是在该位置上放 ...

  7. 笨办法学Python(十四)

    习题 14:提示和传递 让我们使用 argv 和 raw_input 一起来向用户提一些特别的问题.下一节习题你会学习如何读写文件,这节练习是下节的基础.在这道习题里我们将用略微不同的方法使用 raw ...

  8. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  9. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  10. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

随机推荐

  1. 洛谷1541(多维dp)

    走格子拿分数,直接弄dp[i]是到了第i格的最大得分可以发现是假的. 于是此题设f[i][j][k][t]代表四种步伐各用了几次可以得到的最大得分,到达的点可以直接算出来,就好转移了. const i ...

  2. Go语言基础之12--Channel

    一.不同goroutine之间如何进行通讯? 1.全局变量和锁同步 缺点:多个goroutine要通信时,定义太多的全局变量(每个全局变量功能不一样),不好维护 2.Channel 二.channel ...

  3. 深入了解Json Web Token之概念篇

    https://www.freebuf.com/articles/web/180874.html http://www.ruanyifeng.com/blog/2018/07/json_web_tok ...

  4. 练习五十六:for循环

    某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换 方法一: def o ...

  5. thinkPHP Model的操作

    1.建立一个表 create table Demo( -> id int, ), -> age int, ) -> ); 2.新增数据 2.1面向过程的风格 $d = $a-> ...

  6. php数组·的方法3-数组和变量之间的转换

    /* * 数组和变量之间的转换 * */ //extract() 使用数组定义一组变量 // 键名为变量名 键值为变量值(类似于js的解构赋值) // 返回值是数组的长度 echo '<hr&g ...

  7. tomcat直接访问webinfo里的jsp

    <servlet> <servlet-name>requestScope.jsp</servlet-name> <jsp-file>/WEB-INF/r ...

  8. (转)Linux命令:使用dig,nslookup命令解析域名

    Linux命令:使用dig命令解析域名 Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息. dig的全称是 (domain infor ...

  9. vue进阶 --- 实例演示

    这篇博客将通过一个实例来对vue构建项目的过程有一个了解. 主要用到的知识点如下所示: vue-router 2.0路由配置 router-view 和 router-link的使用 transiti ...

  10. 用一层for循环初始化三维数组

    ][][]; ; i < * * ; i++) { a[i / ][(i / ) % ][i % ] = i; printf(, (i / ) % , i % ); // printf(&quo ...