笨办法学Python(二十六)
习题 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(二十六)的更多相关文章
- 笨办法学Python(十六)
习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...
- 笨办法学Python(十二)
习题 12:提示别人 当你键入 raw_input() 的时候,你需要键入 ( 和 ) 也就是“括号(parenthesis)”.这和你格式化输出两个以上变量时的情况有点类似,比如说 "%s ...
- 笨办法学Python(十九)
习题 19: 函数和变量 函数这个概念也许承载了太多的信息量,不过别担心.只要坚持做这些练习,对照上个练习中的检查点检查一遍这次的联系,你最终会明白这些内容的. 有一个你可能没有注意到的细节,我们现在 ...
- 笨办法学Python(十八)
习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...
- 笨办法学Python(十五)
习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...
- 笨办法学Python(十)
习题 10: 那是什么? 在习题 9 中我你接触了一些新东西.我让你看到两种让字符串扩展到多行的方法.第一种方法是在月份之间用 \n (back-slash n )隔开.这两个字符的作用是在该位置上放 ...
- 笨办法学Python(十四)
习题 14:提示和传递 让我们使用 argv 和 raw_input 一起来向用户提一些特别的问题.下一节习题你会学习如何读写文件,这节练习是下节的基础.在这道习题里我们将用略微不同的方法使用 raw ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
随机推荐
- php 安装扩展库
liunx系统 1. /usr/local/php/bin/php-config php 配置文件位置 [ php-config是一个脚本文件,用于获取所安装的php配置的信息 ] 在编译扩展时,如果 ...
- 设置IIS,使客户端访问服务器上的文件
1.打开IIS控制器——"运行-cmd-inetmgr-回车“ 2.选中要设置的网站 3.右键,添加虚拟目录 4.如上图所示,在别名处输入文件目录的名称,物理路径要选择文件在磁盘上的存储根目 ...
- POJ-1128-Frame Stacking
链接:https://vjudge.net/problem/POJ-1128 题意: 每张图片上面画了一些边框,给出这些边框叠在一起后的图片,图片边框一定是由一个字母表示并且每条边至少三个字符,输入保 ...
- CodeForces - 893C-Rumor(并查集变式)
Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...
- day 012 生成器 与 列表推导式
生成器的本质就是迭代器,写法和迭代器不一样,用法一样. 获取方法: 1.通过生成器函数 2.通过各种推导式来实现生成器 3.通过数据的转换也可以获取生成器 例如: 更改return 为 yield 即 ...
- 动态添加表sql
注意:1.tb_wx_userinfo已经存在,直接复制该表结构 DECLARE @manufacturer_id NVARCHAR(10),@sql NVARCHAR(500) SET @manuf ...
- SpEL
Spriing boot stater中根据配置文件中的条件 生成相应的bean, 以适应不同场景 @ConditionalOnExpression中使用SpEl, 支持各种条件表达式 String ...
- eclipse安装阿里规范模板
https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化
- .netcore在linux下使用P/invoke方式调用linux动态库
http://www.mamicode.com/info-detail-2358309.html .netcore下已经实现了通过p/invoke方式调用linux的动态链接库(*.so)文件 1 ...
- mkpasswd的使用
首先安装except包:yum -y install except 参数: -l # (密码的长度定义, 默认是 9) -d # (数字个数, 默认是 2) -c # (小写字符个数, 默认是 2) ...