习题25 更多更多的实践
2016-07-26 11:06
375 查看
def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return words def sort_words(words): """Sort 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 fater 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)
上面是源代码,可以看到定义了很多函数名称。
打字的时候别打错,尤其是 word 和 words ,可恶心人了
关键是运行的时候可能不知道怎么运行,我也查了好久才明白。
运行,第一步直接输入 python
然后按部就班输入即可
最后一步出错就是 words 打成了 word
实在不想改了。。。
======================================================================================================
附加练习
1.要明白如何调用 ex25.py 中的函数
首先要
import ex25然后调用
方法是
ex25.[函数名]这样就可以了
2.执行 help 命令
可以看出,“help”打印出了函数的说明
3. form ex25 import * 可以免去一直输入 ex25.xxxxxx 这样的函数
4.Ctrl+Z 退出Python
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法