更多更多的练习

  • 先建立ex25.py
  • 代码如下
  • 运行代码如下:
  • 结果如下:
  • 巩固练习
  • Tips:

先建立ex25.py

代码如下

def break_words(stuff):"""This function will break up words for us."""words=stuff.split(' ')return wordsdef 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:NAMEex25FUNCTIONSbreak_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.FILEd:\\ex25.pyhelp
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只是打印输出。

笨方法学python 习题25相关推荐

  1. 笨方法学python 习题37

    还是在笨方法学python中... 本节的习题是看一下作者列出的python中的各种运算符,尝试来理解这些符号. 在这里,我只列出了一些自己不会的,通过查百度得到得答案,这里来列举一下. (另外有不怎 ...

  2. 笨方法学python习题4

    变量和命名 #笨方法学python_习题4#定义变量 cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90#计算 cars_not_ ...

  3. python数值运算答案_笨方法学Python 习题3:数字和数学计算

    数字和数学计算 print("I will now count my chickens") print("Hens",25+30/6) print(" ...

  4. python手记(游戏) 笨方法学python习题36【持续更新中】

    如有意见或其他问题可在下方写下评论或加QQ:1693121186 欢迎一起讨论技术问题! 代码如下: 解说:这是笨方法的习题36,让做一个游戏.我会持续更新,如果想复制玩玩的同学,请别将主线线人以下的 ...

  5. 笨方法学Python 习题 42: 对象、类、以及从属关系

    有一个重要的概念你需要弄明白,那就是"类(class)"和"对象(object)"的区别.问题在于,class 和 object 并没有真正的不同.它们其实是同 ...

  6. 《笨办法学python》6_笨办法学Python 习题 25: 更多更多的练习

    我们将做一些关于函数和变量的练习,以确认你真正掌握了这些知识.这节练习对你来说可以说是一本道:写程序,逐行研究,弄懂它. 不过这节练习还是有些不同,你不需要运行它,取而代之,你需要将它导入到 pyth ...

  7. python38使用_笨方法学Python 习题38:列表的操作

    列表的操作: 这里先复习一下之前遇见过的函数:split()通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符 str.split(str="", nu ...

  8. 笨方法学python 习题26

    习题26 python:3.9 首先我不知道为什么书中的网站打不开,一直是404,所以我就去网上涨了源代码,如下 def break_words(stuff):"""Th ...

  9. 笨方法学python 习题34

    习题34 python:3.9 animals = ['bear','python','peacock','kangaroo','whale','platypus']print ("The ...

最新文章

  1. OCA读书笔记(6) - 配置Oracle网络环境
  2. 用户及权限管理功能常规测试
  3. JSON Web Tokens(JWT)
  4. 偷懒大法好,用 selenium 做 web 端自动化测试
  5. Spring简介-Spring发展历程
  6. angularjs全栈开发知乎_1-4【微信小程序全栈开发课程】原生小程序框架介绍
  7. c#语言中的变量名,在C#中创建动态变量名
  8. 的图层类型有哪些_东莞都市领航平面设计培训班都学习哪些内容?
  9. window下远程监控linux下tomcat的jvm
  10. 引导页设计灵感,高大上的设计案例
  11. 耐克人脸识别_人脸识别首案宣判,当人脸识别遇上面试,将碰出怎样的火花?...
  12. 44. Element insertBefore() 方法
  13. Hololens中的虚拟物体通过Vuforia的码实现虚实融合打开hololens像机
  14. iOS-常用宏定义大全
  15. 肌电数据归一化并显示灰度图片
  16. 量子计算机与仿生论文,有关量子力学的论文
  17. 小米平板4twrp一键刷入工具_不要浪费小米10的双扬声器!杜比全景音刷入教程分享...
  18. 怎样关闭计算机亮度自动调节,如何关闭计算机显示器的自动亮度调节功能
  19. 女暴徒BeHype:MINT和Raffle抽奖规则及答疑
  20. python里hist_python matplotlib模块: hist(直方图)

热门文章

  1. Java制作五子棋 V1.0
  2. 一起零基础学Python
  3. QGIS基于多期哨兵2影像遥感指数阈值法提取冬小麦分布(3)-NDVI阈值
  4. MFC的导航窗格浮窗设计
  5. python的实例和类.
  6. android开发包下载
  7. Linux查看DNS
  8. 视频转码编辑工具:Compressor for Mac(4.5.4)
  9. 基于ZigBee的出租车调度系统
  10. 【前缀树】C++ 数据结构 字典树