博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
笨方法学Python,Lesson 26
阅读量:6908 次
发布时间:2019-06-27

本文共 4473 字,大约阅读时间需要 14 分钟。

hot3.png

本节习题是找Bug

修改前代码

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(-1    print 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 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 - 5print "This should be five: %s" % fivedef 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_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)    print worddef print_last_word(words):    """Prints the last word after popping it off."""    word = words.pop(-1)    print 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 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 - 6print "This should be five: %s" % fivedef 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 crates." % secret_formula(start_point)sentence = "All good things come to those who weight."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_wordsprint_first_and_last(sentence)print_first_and_last_sorted(sentence)

输出

100707_8nBh_2297516.png

转载于:https://my.oschina.net/u/2297516/blog/522464

你可能感兴趣的文章
C语言 指针练习-直接插入排序法
查看>>
2.Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC)-
查看>>
7.zookeeper集群搭建(windows环境下)
查看>>
spring中aop的注解实现方式简单实例
查看>>
Java发送邮件Utils
查看>>
JFreeChart与struts2整合实例
查看>>
利用Arraylist数组简单实现随机双色球Demo
查看>>
使用OpenCV&&C++进行模板匹配
查看>>
P3007 [USACO11JAN]大陆议会The Continental Cowngress(2-SAT)
查看>>
P5169 xtq的异或和(FWT+线性基)
查看>>
libcurl以get方式请求服务器端文件
查看>>
nginx正向代理和反正代理区别
查看>>
bzoj3946: 无聊的游戏
查看>>
CentOS上安装Git服务器
查看>>
斐波那契数列
查看>>
Codeforces 534B - Covered Path
查看>>
AJAX 一些常用方法
查看>>
使用ifstream和getline读取文件内容[c++]
查看>>
洛谷 P2391 白雪皑皑(并查集)
查看>>
修改dedecms中某个页面ueditor的宽度
查看>>