笨办法学python3 学习笔记 习题25

习题25 更多更多的练习# 定义break_words函数def break_words(stuff): # 加注释 """This function will break up words for us.""" # 将sutff按空格分隔——words words = stuff.split(' ') return words# 定义sort_wo...
摘要由CSDN通过智能技术生成

习题25 更多更多的练习

# 定义break_words函数
def break_words(stuff):
    # 加注释
    """This function will break up words for us."""
    # 将sutff按空格分隔——words
    words = stuff.split(' ')
    return words

# 定义sort_words函数
def sort_words(words):
    """sorts the words."""
    # 将words内的元素排序,并将排序后的内容返回
    return sorted(words)

# 定义print_first_words函数
def print_first_word(words):
    """prints the first word after popping it off."""
    # 将words的第一个元素删除,并将该元素赋值给word
    word = words.pop(0)
    print(word)

# 定义print_first_words函数
def print_last_word(words):
    """prints the last word after popping it off."""
    # 将words的最后一个元素删除,并将该元素赋值给word
    word = words.pop(-1)
    print(word)

# 定义sort_sentence函数
def sort_sentence(sentence):
    """Takes in a full sentence and returens the sorted words."""
    # sentence为参数,运行行1的函数,赋值给words
    words = break_words(sentence)
    # words为参数,运行行11的函数,并返回
    return sort_words(words)

# 定义print_first_and_last函数
def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    # sentence为参数,运行行1的函数,赋值给words
    words = break_words(sentence)
    # words为参数,运行行17的函数
    print_first_word(words)
    # words为参数,运行行25的函数
    print_last_word(words)

# 定义print_first_and_last_sorted函数
def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    # sentence为参数,运行行33的函数,结果赋值给words
    words = sort_sentence(sentence)
    # words为参数,运行行17的函数
    print_first_word(words)
    # words为参数,运行行25的函数
    print_last_word(words)
PS D:\pythonp> python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
>>> words
['All', 
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值