ex24
快速打字和学习英语的重要性
ex25 更多更多的实践
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): '''Print the first word after poping it off.''' word = words.pop(0) print word def print_last_word(words): '''Prints the last word after poping 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): '''Print 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 firsr and last one.''' words = sort_sentence(sentence) print_first_word(words) print_last_word(words)
需要注意:def 变量 之后跟引号,然后下一行缩进。
import ex25 → ex25 作为一个模块出现不需要.py的后缀,在这个模块里定义的函数也可以直接调用出来
运行终端时出现的提示:
unexpected unindent : 缩进不正确
invalid syntax : 语法错误 (可能是漏掉引号或者括号)
ex26 恭喜你,现在可以考试了!
import ex25def 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 - 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_point)sentence = "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)
主要的问题:
用到了ex25中的函数 没有import
漏引号、半个括号、关键词拼写错误
缩进错误
ex27 记住逻辑关系
逻辑术语、真值表、True、False
ex28 布尔表达式练习
"abc" == 1、"abc" == 0、"abc" == "bc" 都是 False
"abc" and "EFG" 返回 " abc",1 and 1 返回 1 。 因为都是返回两个被操作对象中的一个,而非他们的布尔表达式True或者False,而且是第一个操作数(abc)
短路逻辑:任何以 False 开头的 and 语句都是直接处理成 False,不会检查后面的语句, True、or 同理
ex29-30 if、else和if
多个 elif 块都是 True 时 Python 只会运行它遇到的是 True 的第一个块,所以只有第一个 True 的块会运行。
people = 30cars = 40buses = 15if cars > people: print "We should take the cars."elif cars < people: print "We should not take the cars."else: print "We can't decide."if buses > cars: print "That's too many buses."elif buses < cars: print "Maybe we could take the buses."else: print "We still can't decide."if people > buses: print "Alright, let's just take the buses."else: print "Fine, let's stay home then."
ex31 作出决定
print "You enter a dark room with two doors. Do you go through door #1 or #2?"door = raw_input("> ")if door == "1" : print "There's a giant bear here eating a cheese cake. What do you do?" print "1. Take the cake." print "2. Scream at the bear." bear = raw_input("> ") if bear == "1" : print "The bear eats your face off. Good job! " elif bear == "2 " : print "The bear eats your legs off. Good job! " else : print "Well, doing %s is probably better. Bear runs away." %bear elif door == "2" : print "You stare into the endless abyss at Cthlhu's retina" print "1. Blueberries." print "2. Yellow jacket clothspins." print "3. Understanding revolvers yelling melodies." insanity = raw_input("> ") if insanity == "1" or insanity == "2": print "Your boby survives powered by a mind of jello. Good job!" else: print "The insanity rots your eyes into a pool of muck. Goof job!" else: print "Your stumble around and fall on a knife and die. Good job!"
注意: if、elif、else 需要接引号, 然后转行缩进
怎么判断一个数出于某个值域中?
1,使用 1 < x < 10 2,用 x in range(1, 10)