您的位置:首页 > 编程语言 > Python开发

习题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