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 支持英文的单词纠错 可 ...
随机推荐
- 图片溢出div问题的最终解决方案
2016.11.20备注: 此问题通过css的max-width:100%;即可解决. 前两天编写了一个前端页面,在本机上显示一切正常.不过在不断的测试中,发现了一个严重的问题,如果图片过大,会撑破d ...
- MultiProvider
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- js之oop <四>对象管理
对象扩展管理 Object.isExtensible() 检测对象是否可扩展(一般返回true).Object.preventExtensions() 防止对象扩展. var p = {p1:&quo ...
- 使用Nexus创建Maven私服
Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问.利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact.Nexus ...
- python的class的__str__()和__repr__()函数
repr(object) 返回一个可以用来表示对象的可打印字符串首先,尝试生成这样一个字符串,将其传给 eval()可重新生成同样的对象 否则,生成用尖括号包住的字符串,包含类型名和额外的信息(比如地 ...
- jquery的colorbox关闭并传递数据到父窗
function closebox(para1, para2) { var k = parent;// 父窗口对象 k.document.getElementById("para1" ...
- Informix 11.5 SQL 语句性能监控方法及实现
我们知道,在数据库应用系统中,SQL 语句的性能好坏至关重要.如果 SQL 语句性能很差,可能会导致整个数据库应用系统的性能也非常差.那么,如何监控数据库系统中 SQL 语句的性能,导致 SQL 语句 ...
- 导出Excel 有身份证时注意
if (this.GridView1.Rows.Count != 0) { HttpContext.Current.Response.Clear() ...
- nulls last ratio_to_report(id) over() 占比函数
ORDER BY t3.pctl DESC NULLS LAST http://blog.itpub.net/9932141/viewspace-600751/ http://blog.csdn.n ...
- UNION并集运算
在集合论中,两个集合(集合A和集合B)的并集是一个包含集合A和B中所有元素的集合.换句话说,如果一个元素属于任何一个输入集合,那么它也属于结果集.如图所示.