笨方法学Python,Lesson 27 - Lesson 31

Exercise 27

本节介绍True 和 False,即布尔值,无代码

①记忆真值表

133043_W99g_2297516.png

133043_q7HB_2297516.png

Exercise 28

本节主要是掌握布尔代数运算

①布尔运算,先计算括号,然后是等号、不等号等运算,再计算not,最后计算and和or

②Python布尔操作符还包括:<  小于号

 >  大于号 

<=  小于等于

>=  大于等于 

<>  不等于,将逐渐被弃用,主流是 !=

③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."

输出

153726_mP4R_2297516.png

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."

输出

155213_Wgme_2297516.png

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!"

输出

162225_v3jy_2297516.png

Notes:

①if语句中可以继续嵌套if,引导进入不同的分支

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值