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)
时间: 2024-08-11 03:29:22

ex26 纠正练习的相关文章

SharePoint Search之(五)Query spelling correction— 查询拼写纠正

?? Query spelling correction 在使用搜索引擎的时候.假设一不小心输入错误,或者对于某个词语记得不太清楚,搜索引擎会自己主动纠正: 这个功能可以缩短用户的时间,很好用.在SharePoint 2013中.通过配置Search dictionary  能够实现类似的功能. SharePoint 2013自己维护了一个拼写词典.叫做默认拼写词典(default spelling dictionaries).这个拼写词典是SharePoint自己维护的,用户不能改动. 假设用

[翻译]纠正PostCSS的4大认识误区

市面上已经有很多的前端工具,再来引入新的前端工具,价值大不大?这主要取决于,它是否给开发人员提供了新的功能,是否值得花时间和精力去学习和使用? PostCSS出现时有一个很有趣的现象.像sass和less这些主要做css预处理的工具,已经有了它们的类别和使用方法的区别. 让我们先解决一些PostCSS常见的认识误区,这样可以看出它是如何提高你的工作效率,改善你的css编码工作的. 提示:如果你想了解PostCSS是什么及如何安装使用它,请看<PostCSS简介>,然后再回来看这篇文章. 误区1

英语发音纠正

英语发音纠正 1. 为了空出时间,让你的舌头和嘴唇找到这9个准确的位置. 1. Speak slowly 2. get into position 3. take in air 4. force out air 5. make every word have a long vowe 2. 10种口型: 1. AEIOU: answer often in up 2. BMP: best mention patient 3. CKG: can cake gone 4. DJNT: duck cake

“无法嵌入互操作类型。。。”的错误纠正方法

引用COM组件的时候,出现了“无法嵌入互操作类型……,请改用适用的接口”的错误提示. 解决方法: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False. 仅做一笔记而已. "无法嵌入互操作类型..."的错误纠正方法,布布扣,bubuko.com

LED显示屏灰度等级的提高及非线性纠正

http://www.doc88.com/p-09193698596.html之原文 作者:杨赓 上海中大高新电子技术有限公司 摘要:本文从增加LED元色调灰深度着手对LED显示屏的非线性灰度纠正提出了不同的解决方法,从而彻底改变LED显示屏显示效果不佳的现状,深入实施可望带动中国整个LED产业,使LED产品的图象品质步入新的时代 关键词: 显示:LED:非线性纠正: 一.引言 LED显示屏作为大型显示设备的一种,具有亮度高.价格低.寿命长.维护简便等优点.带有灰度效果的LED显示屏较之普通LE

WPF(C#)图片色彩的纠正-上

WPF(C#)图片色彩的纠正-上 WPF(C#)图片色彩的纠正-下 前言 对图片进行色彩的纠正,其实与WPF是没有什么关系的,为什么标题又是“WPF(C#)图片色彩的纠正”呢,因为这些图片色彩的纠正功能都是承载在WPF界面上的,并且我也很想介绍一些关于WPF方面的知识,所以就命名了此标题. 这个软件的主要功能是通过设置Tint, BlackEnhance, ColorVibrancy, Automatic, Sharpen, Depth,Bias等参数来对图片的色彩进行纠正(参数属于专业领域词汇

ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的两种解决方案

demo下载地址:http://pan.baidu.com/s/1c0eROkS 方案一:利用UINavigationBar 自带的布局item(Fixed space Bar Button Item)来适配ios7. 其实就是在原来返回按钮的位置上在多加一个占位的Item 为UINavigationBar 写一个分类.UINavigationItem+correct_offset.h #import <UIKit/UIKit.h> @interface UINavigationItem (c

口腔溃疡要从哪几方面来纠正生活习惯呢

口腔溃疡要从哪几方面来纠正生活习惯呢? 1.平时要纠正挑食偏食习惯,饮食不宜过精,多摄入富含B族维生素类.微量元素锌.铁的食物. 2. 要避免口腔溃疡维生素,还应注意避免其他的引发因素,如避免用含十二烷基硫酸钠的牙膏.刷牙时用力不宜过猛以免弄伤牙龈或口腔,避免外物伤及口腔,吃东西时细嚼慢咽小心牙齿咬伤舌头或颊,戴假牙者应注意避免不适配的假牙长期擦伤,不食用过烫的食物,避免食用过敏食物,注意避免尖锐食物的擦伤,改正咬唇舌颊的不良习惯等. 3.平时长期用药者,出现口腔溃疡时,应考虑药物的原因.可引起

百度地图gps信息纠正

如题,用手机百度获取到的gps相关坐标信息与真实坐标信息有一定的偏差存在,至于为啥偏差,暂且深究. 下面事我搜索到的将误差减小到最小化的方法,仅供参考. baidu经纬度坐标与google经纬度坐标都是经过转换的.使用下面那个url可以将原始坐标或者谷歌的坐标转换成baidu的坐标http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=114.324821&y=22.605812,将得到的base64字符,还原就是真实的经纬度