《笨办法学python》加分习题25——我的答案

这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!

文中例题:

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.pop(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)

文中运行使用的是shell,由于我想保存下运行代码,我就再编辑了一份可以运行ex25.py的ex25_run.py。功能和文中shell所使用的一致,只是在错误wrods那里,因为不能运行所以就没有那一行了。

ex25_run.py:

import ex25

sentence = "All good things come to those who wait."
words = ex25.break_words(sentence)
print words

sorted_words = ex25.sort_words(words)
print sorted_words

ex25.print_first_word(words)

ex25.print_last_word(words)

print words

ex25.print_first_word(sorted_words)

ex25.print_last_word(sorted_words)

print sorted_words

sorted_words = ex25.sort_sentence(sentence)
print sorted_words

ex25.print_first_and_last(sentence)

ex25.print_first_and_last_sorted(sentence)

运行结果:

/usr/bin/python2.7 /home/yxwang/Desktop/pystu/20170821/ex25_run.py
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
All
wait.
['good', 'things', 'come', 'to', 'those', 'who']
All
who
['come', 'good', 'things', 'those', 'to', 'wait.']
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
All
wait.
All
who

截图太长,不懂怎么截图,就复制当代码粘贴了。

习题答案:

1、我这里讲的行数针对我上文提到的ex25_run.py

  • 16-18行和10-12行功能一致,打印并且去掉那个位置的列表元素
  • 20行 打印剩下的列表元素
  • 22行 sorted_words赋值为排序过的列表
  • 23行 打印出来
  • 25行是个复合函数,这个函数内行另外三个函数,作用和之前一样
  • 27行如上。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
"笨办法"Python(第3版)是一本Python入门书籍,适合对计算机了解不多,没有过编程,但对编程感兴趣的读者习使用。这本书以习题的方式引导读者一步一步习编程,从简单的打印一直讲到完整项目的实现,让初者从基础的编程技术入手,最终体验到软件开发的基本过程。 本书结构非常简单,共包括52个习题,其中26个覆盖了输入/输出、变量和函数三个主题,另外26个覆盖了一些比较高级的话题,如条件判断、循环、类和对象、代码测试及项目的实现等。每一章的格式基本相同,以代码习题开始,按照说明编写代码,运行并检查结果,然后再做附加练习。 Zed Shaw完善了这个堪称世上最好的Python习系统。只要跟着习,你就会和迄今为止数十万Zed教过的 初者一样获得成功。 在这本书中,你将通过完成52个精心设计的习题Python。阅读这些习题,把习题的代码精确地写出来(禁止复制和粘贴!),修正你的错误,观察程序的运行。在这个过程中,你将了解软件是如何工作的,好的程序看起来是什么样子,怎样阅读、编写、思考代码,以及如何用专业程序员的技巧来找出并修正错误。最重要的是,你将到下面这些编写优秀的Python软件必需的初始技能。 这本书会让你的每一分钟投入都有回报。Python是世界上最强大、最受欢迎的编程语言之一,很快你就会成为一名Python程序员。 "笨办法"Python(第3版)目录 习题0 准备工作 1 习题1 第一个程序 7 习题2 注释和#号 12 习题3 数字和数计算 14 习题4 变量和命名 17 习题5 更多的变量和打印 20 习题6 字符串和文本 23 习题7 更多打印 26 习题8 打印,打印 28 习题9 打印,打印,打印 30 习题10 那是什么 32 习题11 提问 35 习题12 提示别人 37 习题13 参数、解包和变量 39 习题14 提示和传递 42 习题15 读取文件 45 习题16 读写文件 48 习题17 更多文件操作 51 习题18 命名、变量、代码和函数 54 习题19 函数和变量 57 习题20 函数和文件 60 习题21 函数可以返回某些东西 63 习题22 到现在你到了哪些东西 66 习题23 阅读一些代码 67 习题24 更多练习 68 习题25 更多更多的实践 71 习题26 恭喜你,现在可以考试了! 75 习题27 记住逻辑关系 76 习题28 布尔表达式练习 79 习题29 if语句 82 习题30 else和if 84 习题31 作出决定 86 习题32 循环和列表 89 习题33 while循环 93 习题34 访问列表的元素 96 习题35 分支和函数 98 习题36 设计和调试 102 习题37 复习各种符号 104 习题38 列表的操作 109 习题39 字典,可爱的字典 113 习题40 模块、类和对象 118 习题41 习面向对象术语 124 习题442 对象、类及从属关系 129 习题43 基本的面向对象分析和设计 134 习题44 继承与合成 147 习题45 你来制作一个游戏 156 习题46 项目骨架 160 习题47 自动化测试 166 习题48 更复杂的用户输入 170 习题49 创建句子 175 习题50 你的第一个网站 180 习题51 从浏览器中获取输入 187 习题52 创建Web游戏 198

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值