Exercise 27python
本节介绍True 和 False,即布尔值,无代码less
①记忆真值表ide
Exercise 28spa
本节主要是掌握布尔代数运算code
①布尔运算,先计算括号,而后是等号、不等号等运算,再计算not,最后计算and和orci
②Python布尔操做符还包括:< 小于号input
> 大于号 it
<= 小于等于class
>= 大于等于 im
<> 不等于,将逐渐被弃用,主流是 !=
③Python布尔运算短路逻辑:任何以 False 开头的 and 语句都会直接被处理成 False 而且不会继续检查后面语句了。任何包含 True 的 or 语句,只要处理到 True 这个字样,就不会继续向下推算,而是直接返回 True 了。不过仍是要确保整个语句都能正常处理,以方便往后理解和使用代码。
Exercise 29
代码
people = 20 cats = 30 dogs = 15 if people < cats: print "Too many cats! The world is doomed!" if people > cats: print "Not many cats! The world is saved!" if people < dogs: print "The world is drooled on!" if people > dogs: print "The world is dry!" dogs += 5 if people >= dogs: print "People are greater than or equal to dogs." if people <= dogs: print "People are less than or equal to dogs." if people == dogs: print "People are dogs."
输出
Notes:
①本节开始引入if语句。注意if后的冒号和4个空格的缩进。
Exercise 30
代码
people = 30 cars = 40 trucks = 15 if cars > people: print "We should take the cars." elif cars < people: print "We should not take the cars." else: print "We cann't decide." if trucks > cars: print "That's too many trucks." elif trucks < cars: print "Maybe we could take the trucks." else: print "We still can't decide." if people > trucks: print "Alright, let's just take the trucks." else: print "Fine, let's stay home then."
输出
Notes:
①elif语句提供if的分支块
②当Python遇到第一个为True的语句块并执行后,自动忽略后面因此elif语句,跳过整个if模块
③当因此句块均不符合条件时,执行else语句块。
Exercise 31
代码
print "You enter a dark room with two doors. Do you go through door #1 or door #2?" door = raw_input(">") if door == "1": print "There is 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 Cthulhu's retina." print "1. Blueberries." print "2. Yellow jacket clothespins." print "3. Understanding revolvers yelling melodies." insanity = raw_input("> ") if insanity == "1" or insanity == "2": print "Your body survives powered by a mind of jello. Good job!" else: print "The insanity rots your eyes into a pool of muck. Good job!" else: print "You stumble around and fall on a knife and die. Good job!"
输出
Notes:
①if语句中能够继续嵌套if,引导进入不一样的分支