笨方法学python 习题25

部署运行你感兴趣的模型镜像

先建立ex25.py

代码如下

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):
    """Print 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)

运行代码如下:

import ex25
sentence="All good things come to those who wait."
words=ex25.break_words(sentence)
words
sorted_words=ex25.sort_words(words)
sorted_words
ex25.print_first_word(words)
ex25.print_last_word(words)
words
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
sorted_words
sorted_words=ex25.sort_sentence(sentence)
sorted_words
ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)

结果如下:

All
wait.
All
who
All
wait.
All
who

进程已结束,退出代码0

巩固练习

1. 使用help(ex25)和help(ex25.break_words)
帮助文档就是在定义函数时放在"""之间的字符串,也成为注释

words=ex25.break_words(sentence)
words
sorted_words=ex25.sort_words(words)
sorted_words
ex25.print_first_word(words)
ex25.print_last_word(words)
words
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
sorted_words
sorted_words=ex25.sort_sentence(sentence)
sorted_words
ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)
help(ex25)
print('help')
help(ex25.break_words)
print('help')

结果如下:

All
wait.
All
who
All
wait.
All
who
Help on module ex25:

NAME
    ex25

FUNCTIONS
    break_words(stuff)
        This function will break up words for us.
    
    print_first_and_last(sentence)
        Prints the first and last words of the sentence.
    
    print_first_and_last_sorted(sentence)
        Sorts the words then prints the first and last one.
    
    print_first_word(words)
        Prints the first word after popping it off.
    
    print_last_word(words)
        Print the last word after popping it off.
    
    sort_sentence(sentence)
        Takes in a full sentence and returns the sorted words.
    
    sort_words(words)
        Sorts the words.

FILE
    d:\\ex25.py


help
Help on function break_words in module ex25:

break_words(stuff)
    This function will break up words for us.

help

进程已结束,退出代码0

Tips:

1. 有的函数打印出来是none
漏写了return,回到代码check
2. 输入import ex25时显示-bash:import:command not found.
这不是在终端运行的代码,你需要先运行python再录入代码
3. 输入import ex25时显示ImportError:No module named ex25.py
.py是不需要的,python知道文件是.py结尾,只需要输入import ex25
4. 运行时提示SyntaxError: invalid syntax
语法错误,是不是少了括号,或者其他语法问题。按照报错提示寻找语法错误
5. 为什么word.pop这个函数会改变words变量内容?
words是一个列表,可以对它进行操作,结果也可以被保存
6. 什么时候用print,什么时候用return?
return会给调用函数的代码给一个结果。思路:函数通过参数接收输入,通过return返回输出。print只是打印输出。

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值