Learn Python The Hard Way(25)
2015-04-23 11:46
381 查看
We're going to do some more practice involving functions and variables to make sure you know them well. This exercise should be straight forward for you to type in, break down, and understand.
However, this exercise is a little different. You won't be running it. Instead you will import it into your python and run the functions yourself.
First, run this like normal with python ex25.py to find any errors you have made. Once you have found all of the errors you can and fixed them, you will then want to follow the WYSS section to complete the exercise.
However, this exercise is a little different. You won't be running it. Instead you will import it into your python and run the functions yourself.
#!/usr/bin/env python # -*- coding: utf-8 -*- 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 )
First, run this like normal with python ex25.py to find any errors you have made. Once you have found all of the errors you can and fixed them, you will then want to follow the WYSS section to complete the exercise.
相关文章推荐
- Learn Python The Hard Way学习(43) - 来自行星Percal #25的Gothons
- Learn Python The Hard Way学习(25) - 甚至更多的练习
- Learn Python The Hard Way-Ecercise21~25
- 【Python】Learn Python the hard way, ex8 格式化字符串
- 【Python】Learn Python the hard way, ex14 argv参数传值
- Learn Python The Hard Way学习(10) - 那是什么?
- Learn Python The Hard Way学习(15) - 读取文件
- Learn Python the Hard Way
- Learn Python The Hard Way, 2nd Edition
- 学《learn python the hard way》(1)
- 笨办法学 Python (Learn Python The Hard Way)
- Learn Python The Hard Way (python 2.7) ex43.py 学完,分析注释
- learn python the hard way(笨办法学python) 附录 个人整理
- learn python the hard way(笨办法学python) 练习38 附加练习
- Learn Python The Hard Way 总结与勘误
- 【Python】Learn Python the hard way, ex3 运算符,浮点数
- Learn Python The Hard Way exercise 42
- 笨办法学python 粗略笔记(learn python the hard way)
- 【Python】Learn Python the hard way, ex32 for循环
- Learn Python The Hard Way-ex9