复习PythonDay24
发布日期:2021-07-01 03:03:04
浏览次数:2
分类:技术文章
本文共 2709 字,大约阅读时间需要 9 分钟。
复习Python的第二十四天
习题 26: 恭喜你,现在可以考试了
作者的话:
当你成为程序员以后,你将需要经常面对别的程序员的代码,也许还有他们的傲慢态度,他们会经常说自己的代码是完美的。
这样的程序员是自以为是不在乎别人的蠢货。优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能,他们会先假设是自己的代码有问题,然后用排除法清查所有可能是自己有问题的地方, 最后才会得出“这是别人的错误”这样的结论。
而本节的习题就是修改现有的代码。
错误排除清单:- 随机删掉的一些字符,以及添加的一些错误;有
- 大部分的错误可以通过Python执行告知; 最可行
- 还有一些算术错误要自己找出来; 已找出
- 再剩下来的就是格式和拼写错误。 有
接下来的是本节的代码:
1. 2. 修改后的代码: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 worddef 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 worldwith logic so firmly plantedcannot discern \n the needs of lovenor comprehend passion from intuitionand requires an explantion\n\t\twhere there is none."""print "--------------"print poemprint "--------------"##### five = 10 - 2 + 3 - 5 # 6print "This should be five: %d" % five#####def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, cratesstart_point = 10000beans, jars, crates = secret_formula(start_point)print "With a starting point of: %d" % start_pointprint "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)##### import ex25sentence = "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)print sorted_wordsprint_first_and_last(sentence)print_first_and_last_sorted(sentence)
比起手动找错误,我更想学pdb自动测试啊,还有调试代码的技巧。
本节的测试很简单,细心点就可以了,本来我以为自己看一遍就差不多了,没想到还是漏了几个错误(汗颜)。
转载地址:https://memcpy0.blog.csdn.net/article/details/79839638 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月13日 00时42分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ImageView scaleType
2019-05-01
RecyclerView notifyItem闪屏问题
2019-05-01
dubbo学习笔记 十一 dubbo-rpc之模块
2019-05-01
motan学习笔记 五 opentracing学习入门
2019-05-01
求列表最长子序列
2019-05-01
字符串的排序
2019-05-01
内存分配(mallloc,calloc,realloc,new)
2019-05-01
网络编程之 Socket函数 (二)
2019-05-01
网络编程之 Socket的模式(一) --- “阻塞/非阻塞” 与 “同步/异步”
2019-05-01
ffmpeg & mplayer & vlc 手册
2019-05-01
Go语言并发组件
2019-05-01
简析STUN协议
2019-05-01
使用 Minidumps 和 Visual Studio .NET 进行崩溃后调试
2019-05-01
Debug 和 Release 编译方式的本质区别
2019-05-01
struts返回xml数据例子
2019-05-01
内存对齐详解
2019-05-01
秋招总结(一)-C++归纳
2019-05-01
秋招总结(三)-操作系统归纳
2019-05-01
带缓冲I/O 和不带缓冲I/O的区别与联系
2019-05-01
LINUX CP命令详解
2019-05-01