题目中给出的代码如下:

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)
#这里面函数变量写错了,应该为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)
#本练习中没引进ex25,所有有两种修改方法import ex25,我选用的是去掉ex25.
print_first_word(words) print_last_word(words) .print_first_word(sorted_words)
#本行多了一个.
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
#本练习中没引进ex25,所有有两种修改方法import ex25,我选用的是去掉ex25.
prin sorted_words
#上面的关键字print错误
print_irst_and_last(sentence) print_first_a_last_sorted(senence)
#上面一行缩进存在问题

修正后

#!/usr/bin/env python
# -*- coding:utf-8 -*- def break_words(stuff):
"""This function will break up words for us."""
b_words = stuff.split(' ')
return b_words def sort_words(s_words):
"""Sorts the words."""
return sorted(s_words) def print_first_word(pf_words):
"""Prints the first word after popping it off."""
pf_word = pf_words.pop(0)
print pf_word def print_last_word(pl_words):
"""Prints the last word after popping it off."""
pl_word = pl_words.pop(-1)
print pl_word def sort_sentence(in_sentence):
"""Takes in a full sentence and returns the sorted words."""
out_words = break_words(in_sentence)
return sort_words(out_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(pf_sentence):
"""Sorts the words then prints the first and last one."""
pf_words = sort_sentence(pf_sentence)
print_first_word(pf_words)
print_last_word(pf_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(d_started):
d_jelly_beans = d_started * 500
d_jars = d_jelly_beans / 1000
d_crates = d_jars / 100
return d_jelly_beans, d_jars, d_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 /= 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_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)

ex26 纠正练习的更多相关文章

  1. C#图片色彩的纠正-上

    WPF(C#)图片色彩的纠正-上 WPF(C#)图片色彩的纠正-下 前言 对图片进行色彩的纠正,其实与WPF是没有什么关系的,为什么标题又是“WPF(C#)图片色彩的纠正”呢,因为这些图片色彩的纠正功 ...

  2. 对属性NaN的理解纠正和对Number.isNaN() 、isNaN()方法的辨析

    1.属性NaN的误解纠正 NaN (Not a Number)在w3c 中定义的是非数字的特殊值 ,它的对象是Number ,所以并不是任何非数字类型的值都会等于NaN,只有在算术运算或数据类型转换出 ...

  3. CSS下拉列表错误纠正

    上一篇关于CSS制作下来列表的错误纠正. 在上一篇中,用CSS只做了下拉列表,但是鼠标不放在导航栏上的时候,下拉列表也是出来的.具体错误就是 div ul{ list-style:none; max- ...

  4. C# winform 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误

    winform中有时添加了新控件之后编译会报错: 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误,如图: 解决方案: 1.“解决方案”→“批生成”→“清理”→“确定”: 2.“解决方案”→ ...

  5. JAVA classpath, 纠正我一直以来错误的认知

    如何调在CLI中使用java tool(JDK中的java命令)调用一个打包在jar中的类,我想大多数人都能给出笼统的方案: java -classpath xxxxx com.test.classA ...

  6. VS2008 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。

    提示这个错误,自己的程序是在VS2008下编译的C/C++ win32程序,自己当时在win7上开发测试,都没有问题,正常使用,也在另一台xp系统上也试了,都没有问题.就发给客户了,没想到有些客户竟然 ...

  7. 关闭 Mac 拼写自动纠正与横线转换

    如果你是个程序员, 如果你恰好用 mac 自带的 notes 来做笔记, 很大可能性, 你会在里面贴代码, 但是, Mac 的拼写检查和自动纠正功能,会把代码变成你不想要的样子, 比如, 它会为你首字 ...

  8. 命令纠正工具 thefuck 的简单使用

    在unix系列的系统中,总会出现 命令拼写或者执行错误的情况, 比如 把 python 写成了pythou, cd 到一个不存在的目录,执行任务的权限 不够的问题, 这是心里 总是 在 fuck,但是 ...

  9. java 英文单词拼写纠正框架(Word Checker)

    Word Checker 本项目用于单词拼写检查. 项目简介 word checker 用于单词拼写检查. Github 地址 特性说明 支持 i18n 错误提示支持 i18N 支持英文的单词纠错 可 ...

随机推荐

  1. [问题2014A13] 解答

    [问题2014A13]  解答 先引入两个简单的结论. 结论 1  设 \(\varphi\) 是 \(n\) 维线性空间 \(V\) 上的线性变换, 若存在正整数 \(k\), 使得 \(\math ...

  2. Lucas定理

    Lucas' theorem In number theory, Lucas's theorem expresses the remainder of division of the binomial ...

  3. 《K&R》里贯穿全书的代码

    个人阅读<K&R>的感觉就是:前后内容联系特别紧密,前面的代码没有理解好到了后面就看不下去. 1.getline(char s[], int lim) 调用结果:往参数数组中读入字 ...

  4. 快速排序,C语言实现

    排序法里比较出名的,具体的算法看下图: 这篇博客说的通俗易懂:http://blog.csdn.net/morewindows/article/details/6684558 这是快速排序的基础,用代 ...

  5. CAD迷你看图

    CAD迷你看图http://www.aec188.com/CAD迷你看图 2016R12超快.超小的CAD多功能看图工具,完全脱离AutoCAD浏览R14-R2016各版本DWG/DXF/DWF的二三 ...

  6. Spring中映射Mongodb中注解的解释

    spring-data-mongodb中的实体映射是通过MongoMappingConverter这个类实现的.它可以通过注释把java类转换为mongodb的文档. 它有以下几种注释: @Id - ...

  7. linux 下mysql的安装,并设置必要的密码

    首先,我使用的是redhat linux ,版本号为: [root@localhost init.d]# cat /proc/version Linux version - (bhcompile@po ...

  8. jquery 源码解析

    静态与实力方法共享设计 遍历方法 $(".a").each() //作为实例方法存在 $.each() //作为静态方法存在 Jquery源码 jQuery.prototype = ...

  9. Java:Collection List Set

    Java:集合 常见集合:List Set List 特点:元素是有序的,而且元素可以重复,因为该集合体系有索引. 常见的三个子类:ArrayList.LinkedList.Verctor List集 ...

  10. 介绍开源的.net通信框架NetworkComms框架之五 心跳检测

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...