习题23:读代码
话一周的时间阅读代码,这。。。其实每天都有在读各种深度网络只不过看不懂(汗颜)
几个网站
习题24:更多练习
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 explanation
\n\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 * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
print("With a starting point of:%d" % start_point)
print("We'd have %d beans, %d jars, and %d crates." %(beans, jars,crates))
start_point = start_point / 10
print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))
习题25:更多更多的练习
主要是关于函数和变量
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) # 把参数words中的内容按照升序排列,并生成一个新的列表,返回结果
def print_first_word(words):
word = words.pop(0) # 拿出words列表中的第一个元素,并返回该元素的值
print(word)
def print_last_word(words):
word = words.pop(-1) # 拿出words列表中的最后一个元素,并返回该元素的值
print(word)
def sort_sentence(sentence):
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
正常运行的时候,不报错,但没有显示
1. 研究答案中没有分析过的行,找出它们的来龙去脉。确认自己明白了自己使用的是模组 ex25 中定义的函数。
2. 试着执行 help(ex25) 和 help(ex25.break_words) 。这是你得到模组帮助文档的方式。 所谓帮助文档就是你定义函数时放在 """ 之间的东西,它们也被称作 documentation comments (文档注解),后面你还会看到更多类似的东西。
3. 重复键入 ex25. 是很烦的一件事情。有一个捷径就是用 from ex25 import * 的方式导入模组。这相当于说:“我要把 ex25 中所有的东西 import 进来。”程序员喜欢说这样的倒装句,开一个新的会话,看看你所有的函数是不是已经在那里了。
from bffLearnPython import *
sentence = "All good things come to those who wait."
words = break_words(sentence) # 对句子划分
print(words) # 输出划分后的单词
sorted_words = sort_words(words) # 对单词进行排序
print(sorted_words) # 输出
print_first_word(words) # 输出word排序中的第一个单词
print_last_word(words) # 输出word排序中的最后一个单词
print(words) # 输出此时的单词
print_first_word(sorted_words) # 输出sort_words排序中的第一个单词
print_last_word(sorted_words) # 输出sort_words排序中的最后一个单词
print(sorted_words) # 输出此时排序完成的单词
sorted_words = sort_sentence(sentence) # 直接对句子进行排序
print(sorted_words) # 输出排序后的单词
print_first_and_last(sentence) # 输出原句子的第一个和最后一个
print_first_and_last_sorted(sentence) # 输出经过排序后句子的第一个和最后一个
可以对比一下在终端下的结果是一样的
4. 把你脚本里的内容逐行通过 python 编译器执行,看看会是什么样子。你可以执行CTRL-D (Windows 下是 CTRL-Z)来关闭编译器。
习题26:恭喜你,现在可以考试了!
修正别人的代码,试一下吧。
代码基本是前面练习的集合,复习一下。
源代码地址:https://learnpythonthehardway.org/exercise26.txt
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):
"""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 - 5
print("This should be five: %s" % five)
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_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 / 10
print("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."
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)
结束这个我也算是完成一半了!加油!