python系统学习日记 L26 修正改错
书中L26是改错习题,对一段有问题的代码进行改正
书中代码:
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.poop(0)print worddef print_last_word(words):"""Prints the last word after popping it off."""word = words.pop(-1print worddef 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)print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""print "--------------"
print poem
print "--------------"five = 10 - 2 + 3 - 5
print "This should be five: %s" % fivedef secret_formula(started):jelly_beans = started * 500jars = jelly_beans \ 1000crates = jars / 100return jelly_beans, jars, cratesstart_point = 10000
beans, jars, crates == secret_formula(start-point)print "With a starting point of: %d" % start_point
print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)start_point = start_point / 10print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pontsentence = "All god\tthings come to those who weight."words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
prin sorted_wordsprint_irst_and_last(sentence)print_first_a_last_sorted(senence)
修改后的代码:
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) #此处pop拼写错误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)print("Let's practice everything.")
print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""print("--------------")
print(poem)
print("--------------")five = 10 - 2 + 3 - 6 #计算公式错误
print("This should be five: %s" % five)def secret_formula(started):jelly_beans = started * 500jars = jelly_beans / 1000 #计算符号错误crates = jars / 100return jelly_beans, jars, cratesstart_point = 10000
beans,jars, crates = secret_formula(start_point) # = 号写成 == start_point拼写成start-pointprint("With a starting point of: %d" % start_point)
print("We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates))start_point = start_point / 10print("We can also do that this way:") #拼写错误少了括号
print("We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point))sentence = "All god\tthings come to those who weight."#ex25. 前缀多余
words = break_words(sentence)
sorted_words = sort_words(words)print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
print(sorted_words) #拼写错误print_first_and_last(sentence) #拼写错误print_first_and_last_sorted(sentence) #拼写错误
输出结果:
python系统学习日记 L26 修正改错相关推荐
- python系统学习日记 L13 参数, 解包, 变量
书中代码: from sys import argvscript, first, second, third = argvprint "The script is called:" ...
- python系统学习日记 L32 循环和列表
书中代码: 这节习题,我们将使用 for循环来创建并打印一些列表.使用 for 循环之前,你需要在某个位置存放循环的结果.最好的方法是使用列表(list),顾名思义,列表就是一个按顺序存放东西的容器. ...
- python系统学习日记 L22~L24 练习
L22~L23:无 L24: 书中代码: print "Let's practice everything." print 'You\'d need to know \'bout ...
- python自学流程-Python系统学习流程图,教你一步步学习python
对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...
- 用python设计一个管理系统思路_Python大佬一个月打造的Python系统学习流程图!
对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...
- Python系统学习流程图, 教你一步步学习python
对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...
- python逻辑运算的一些流程图_Python大牛历时一个月打造的Python系统学习流程图,超详细!...
对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...
- Python系统学习流程图,教你一步步学习python
对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! Pytho ...
- 13位Python大牛历时一个月打造的Python系统学习流程图,超详细!
对于刚开始接触Python的小伙伴来说,没有思路方法,不知道从何开始学习,把软件环境安装好后就不知所措了!接下来我给大家分享下多位大牛倾力打造的python系统学习流程,一个月才设计完的! #Pyth ...
最新文章
- c语言编程学生管理系统的代码,C语言学生管理系统源代码.doc
- python打印浮点数_python – 打印整数或带有n位小数的浮点数
- MIT Molecular Biology 笔记5 转录机制
- thymeleaf加载不了js引用_网站首页加载慢解决方案
- android thread实例
- 将本地Blog部署到GitHub上,有自己的博客页面!
- 你跟大神程序员的差距,就在这8本内功心法
- (组合数学笔记)格点路径问题分析求解
- silverlight-Canvas中1像素的线变成了2像素
- [转][C#]Environment 类
- H3C 路由器的IPSEC ××× 配置
- 【解决方法】jdb2/sdb1-8 io使用过高
- Android8.1 Camera2+HAL3之HIDL open()流程(二十)
- 空巢青年,“空巢”是选择还是无奈? | 数据告诉你
- 普源示波器 电脑 连接 软件_乐高wedo2.0电脑软件安装及蓝牙连接方法
- Dbviusalizer报错Java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Compar
- 远程桌面计算机密码是多少,局域网远程桌面连接密码
- 带登录页面的猜数字小游戏
- 教你使用Python爬虫获取电子书资源实战!喜欢学习的小伙伴过来看啦!
- 传奇人物《周兴和》书连载23 劳燕分飞离故里