题目中给出的代码如下:

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. R----stringr包介绍学习

    1. stringr介绍 stringr包被定义为一致的.简单易用的字符串工具集.所有的函数和参数定义都具有一致性,比如,用相同的方法进行NA处理和0长度的向量处理. 字符串处理虽然不是R语言中最主要 ...

  2. jquery判断对象是否为空并遍历对象

    javascript : if(document.getElementById("target_obj_id")){ } else { } jquery: 因为 $("# ...

  3. 【转载】C++ 与“类”有关的注意事项总结(十二):按成员初始化 与 按成员赋值

    原文:C++ 与"类"有关的注意事项总结(十二):按成员初始化 与 按成员赋值 一.按成员初始化(与构造函数和拷贝构造函数有关) 用一个类对象初始化另一个类对象,比如: Accou ...

  4. ★Java多线程编程总结 系列 转

    下面是Java线程系列博文的一个编目:   Java线程:概念与原理 Java线程:创建与启动 Java线程:线程栈模型与线程的变量 Java线程:线程状态的转换 Java线程:线程的同步与锁 Jav ...

  5. Winform实现右下角弹窗_提示信息

    网页是否经常在电脑右下角弹窗显示消息?其实Winform也是可以实现的.下面介绍两种方法. 第一步:设计窗体 第二步:实现代码 第一种方法 引用user32 声明常量 窗体Load事件 窗体FormC ...

  6. Evolutionary Computing: 4. Review

    Resource:<Introduction to Evolutionary Computing> 1. What is an evolutionary algorithm? There ...

  7. js高级程序设计(七)函数表达式

    定义函数的方式有两种:一种是函数声明,另一种就是函数表达式.函数声明的语法是这样的. function functionName(arg0, arg1, arg2) { //函数体 } Firefox ...

  8. Java for Mac 问题

    卸载jdk 1.7/1.8: sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.pluginsudo rm -fr /Library/P ...

  9. spring随手笔记2:初始化方法

    1.init-method="init" public class HelloWorldServiceImpl implements HelloWorldService { pri ...

  10. oracle with as

    http://blog.csdn.net/a9529lty/article/details/4923957/