ex26 纠正练习
题目中给出的代码如下:
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 纠正练习的更多相关文章
- C#图片色彩的纠正-上
WPF(C#)图片色彩的纠正-上 WPF(C#)图片色彩的纠正-下 前言 对图片进行色彩的纠正,其实与WPF是没有什么关系的,为什么标题又是“WPF(C#)图片色彩的纠正”呢,因为这些图片色彩的纠正功 ...
- 对属性NaN的理解纠正和对Number.isNaN() 、isNaN()方法的辨析
1.属性NaN的误解纠正 NaN (Not a Number)在w3c 中定义的是非数字的特殊值 ,它的对象是Number ,所以并不是任何非数字类型的值都会等于NaN,只有在算术运算或数据类型转换出 ...
- CSS下拉列表错误纠正
上一篇关于CSS制作下来列表的错误纠正. 在上一篇中,用CSS只做了下拉列表,但是鼠标不放在导航栏上的时候,下拉列表也是出来的.具体错误就是 div ul{ list-style:none; max- ...
- C# winform 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误
winform中有时添加了新控件之后编译会报错: 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误,如图: 解决方案: 1.“解决方案”→“批生成”→“清理”→“确定”: 2.“解决方案”→ ...
- JAVA classpath, 纠正我一直以来错误的认知
如何调在CLI中使用java tool(JDK中的java命令)调用一个打包在jar中的类,我想大多数人都能给出笼统的方案: java -classpath xxxxx com.test.classA ...
- VS2008 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。
提示这个错误,自己的程序是在VS2008下编译的C/C++ win32程序,自己当时在win7上开发测试,都没有问题,正常使用,也在另一台xp系统上也试了,都没有问题.就发给客户了,没想到有些客户竟然 ...
- 关闭 Mac 拼写自动纠正与横线转换
如果你是个程序员, 如果你恰好用 mac 自带的 notes 来做笔记, 很大可能性, 你会在里面贴代码, 但是, Mac 的拼写检查和自动纠正功能,会把代码变成你不想要的样子, 比如, 它会为你首字 ...
- 命令纠正工具 thefuck 的简单使用
在unix系列的系统中,总会出现 命令拼写或者执行错误的情况, 比如 把 python 写成了pythou, cd 到一个不存在的目录,执行任务的权限 不够的问题, 这是心里 总是 在 fuck,但是 ...
- java 英文单词拼写纠正框架(Word Checker)
Word Checker 本项目用于单词拼写检查. 项目简介 word checker 用于单词拼写检查. Github 地址 特性说明 支持 i18n 错误提示支持 i18N 支持英文的单词纠错 可 ...
随机推荐
- [问题2014A08] 解答
[问题2014A08] 解答 由假设知 \(f(A)=\mathrm{tr}(AA')\), 因此 \[f(PAP^{-1})=\mathrm{tr}(PAP^{-1}(P')^{-1}A'P')=\ ...
- [问题2014S09] 解答
[问题2014S09] 解答 充分性: 先证明对 Jordan 块 \(J_r(1)\) 以及任意的正整数 \(m\), 均有 \(J_r(1)^m\) 相似于 \(J_r(1)\). 设 \(N ...
- Tomcat内存设置
MyEclipse Tomcat的JDK菜单:“Optional Java VM arguments:”框中输入“有前置空格” -Xms512m -Xmx512m -XX:PermSize=512 ...
- Linux 休眠,挂起(待机),关机等几个命令的区别及如何实现;如何启用Ubuntu的休眠模式
这里对linux 的几个命令整理下,有:休眠,挂起,待机,关机等几个命令的区别及如何实现. 休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将 ...
- [转] 浅谈Linux系统的启动流程
原文:http://blog.csdn.net/justdb/article/details/9621271 版权声明:本文为博主原创文章. Linux系统的启动时通过读取不同的配置文件,执行相应的S ...
- 《Effective C#》:区别和认识四个判等函数
.Net有四个判等函数?不少人看到这个标题,会对此感到怀疑.事实上确是如此,.Net提供了ReferenceEquals.静态Equals,具体类型的Equals以及==操作符这四个判等函数.但是这四 ...
- 在caffe中使用hdf5的数据
caffe默认使用的数据格式为lmdb文件格式,它提供了把图片转为lmdb文件格式的小程序,但是呢,我的数据为一维的数据,我也要分类啊,那我怎么办?肯定有办法可以转为lmdb文件格式的,我也看了一些源 ...
- MFC编程入门之十二(对话框:非模态对话框的创建及显示)
上一节讲了模态对话框及其弹出过程,本节接着讲另一种对话框--非模态对话框的创建及显示. 非模态对话框显示后,程序其他窗口仍然能正常运行,可以响应用户输入,还可以相互切换.上一讲中创建的Tip模态对话框 ...
- Maven3简介
Maven3简介 百度百科 maven官网: http:\/\/maven.apache.org\/ Maven下载安装 Maven 下载:http:\/\/maven.apache.org\/dow ...
- [java基础]分支结构(2)
[java基础]分支结构2 switch case /** 文件路径:G:\JavaByHands\if-else\ 文件名称:switchcase.java 编写时间:2016/6/6 作 者:郑晨 ...