if & else

  • 习题29 - if语句
    • 代码如下:
    • 结果输出:
    • Tips:
      • if语句是什么?有什么用处?
      • +=是什么意思?
  • 习题30 else和if
    • 代码如下:
    • 结果输出:
    • Tips:
  • 习题31
    • 代码如下:
    • 结果输出:
    • Tips:

习题29 - if语句

代码如下:

people=20
cats=30
dogs=15if 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 or dogs.")
if people<=dogs:print("people are less than or equal to dogs.")
if people==dogs:print("people are dogs.")

结果输出:

Too many cats!The world is doomed!
The world is dry!
people are greater than or equal or dogs.
people are less than or equal to dogs.
people are dogs.进程已结束,退出代码0

Tips:

if语句是什么?有什么用处?

每条 if 语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试。Python根据条件测试的值为True还是False来决定是否执行 if 语句中的代码。如果条件测试的值为True,就执行紧跟在 if 语句后面的代码;如果值为False,Python就忽略这些代码。

可用于比较两边条件、数字或者多个条件、特定值是否相等(==) 和不相等(!=),还有大于(>)、大于等于 (>=)、小于 (< )和小于等于(<=)

+=是什么意思?

+=是递增运算符,X += 1 和 X = X + 1 相同,同理±是递减运算符

习题30 else和if

代码如下:

people=30
cars=40
trucks=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 trucks > cars:print("That's too many trucks.")elif trucks < cars:print("Maybe we should 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.")

结果输出:

We should take the cars.
Maybe we should take the trucks.
Alright,Let's just take the trucks.进程已结束,退出代码0

Tips:

  • 单向选择:if…;
  • 双向选择:if…else;
  • 多向选择:if…elif…else;
  • if语句的嵌套


    对于多向选择,程序会从第 1 个 if 语句开始判断,如果第 1 个 if 语句的条件不满足,则判断第 2 个 if 语句的条件……直到满足为止。一旦满足,就会退出整个 if 结构。

    if 语句的嵌套也是很好理解的,就是在 if 或 else 内部再增加一层判断条件。对于 if 语句的嵌套,一层一层从外到内地进行判断就可以了,就像剥洋葱一样.

对于 if 语句,还有以下 3 点需要说明:

  • Python使用的是 elif,而不是 else if
  • Python中 if 的后面不需要加括号
  • Python只有 if 语句,没有 switch 语句,这一点和其他语言不同。

习题31

代码如下:

print("""You enter a dark room with two doors.
Do you go through door #1 or door#2?""")door = input(">")
if door=="1":print("There's a giant bear here eating a cheese cake.")print("1.Take the cake.")print("2.Scream at the bear.")bear=input("<")if bear=="1":print("The bear eats your face off, Good job!")elif bear=="2":print(f"well,doing {bear} is probably better.")print("Bear runs away.")
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=input(">")if insanity=="1"or insanity=="2":print("Your body survives powered by a mind of jello.")print("Good job!")else:print ("The insanity rots your eyes into a pool of muck.")print("Good job!")
else:print("You stumble around and fall on a knife and die. Good job!")

结果输出:

You enter a dark room with two doors.
Do you go through door #1 or door#2?
>1
There's a giant bear here eating a cheese cake.
1.Take the cake.
2.Scream at the bear.
<2
well,doing2 is probably better.
Bear runs away.

Tips:

  • 怎么判断一个数是否处于某个值域?
    经典语法:1<x<10或1<=x<10
    第2种方法:用x in rang(1,10)也可以!

笨方法学python 习题29-31相关推荐

  1. 笨方法学python 习题37

    还是在笨方法学python中... 本节的习题是看一下作者列出的python中的各种运算符,尝试来理解这些符号. 在这里,我只列出了一些自己不会的,通过查百度得到得答案,这里来列举一下. (另外有不怎 ...

  2. 笨方法学python习题4

    变量和命名 #笨方法学python_习题4#定义变量 cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90#计算 cars_not_ ...

  3. python手记(游戏) 笨方法学python习题36【持续更新中】

    如有意见或其他问题可在下方写下评论或加QQ:1693121186 欢迎一起讨论技术问题! 代码如下: 解说:这是笨方法的习题36,让做一个游戏.我会持续更新,如果想复制玩玩的同学,请别将主线线人以下的 ...

  4. 笨方法学Python 习题 42: 对象、类、以及从属关系

    有一个重要的概念你需要弄明白,那就是"类(class)"和"对象(object)"的区别.问题在于,class 和 object 并没有真正的不同.它们其实是同 ...

  5. python数值运算答案_笨方法学Python 习题3:数字和数学计算

    数字和数学计算 print("I will now count my chickens") print("Hens",25+30/6) print(" ...

  6. python38使用_笨方法学Python 习题38:列表的操作

    列表的操作: 这里先复习一下之前遇见过的函数:split()通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符 str.split(str="", nu ...

  7. 笨方法学python 习题26

    习题26 python:3.9 首先我不知道为什么书中的网站打不开,一直是404,所以我就去网上涨了源代码,如下 def break_words(stuff):"""Th ...

  8. 笨方法学python 习题34

    习题34 python:3.9 animals = ['bear','python','peacock','kangaroo','whale','platypus']print ("The ...

  9. 笨方法学python 习题32-33

    循环&列表 习题32 循环和列表 代码如下: 结果输出: Tips: 习题33 while循环 代码如下: 结果输出: Tips: 习题32 循环和列表 代码如下: #创建列表 ''' hai ...

最新文章

  1. milp的matlab的案例代码_matlab30个案例分析案例6代码
  2. 经常玩电脑正确的坐姿_初级茶艺师工作技能培训:第一节礼仪(正确的坐姿)...
  3. 再过五分钟,你就懂Oracle的数据库对象了
  4. Java面向对象编程(基础部分)
  5. drools 规则流_约束流–没有Drools规则语言的现代Java约束
  6. 重构随笔——重构的原则
  7. python基础作业第五天
  8. ADB server didn't ACK问题
  9. 编码基本功:类成员要加前缀,这样可以避免同名变量
  10. [2018.07.26 T1] 航线规划
  11. mt4代理服务器存放文件,mt4如何保存设置好的指标?
  12. 刷网课-踩坑——jsDOM获取不到页面元素节点-iframe标签获取元素节点
  13. 视频教程-ArcGIS for Android视频教程-Android
  14. 正则表达式之断言及常用正则表达式
  15. 第二章 西门子数控机床采集方案
  16. 什么软件可以把独家的qlv格式转换成mp4
  17. PICO 开发者管理平台
  18. 一套ASP.NET优惠券领取微信小程序源码(前台+后台)
  19. 【嵌入式Linux驱动开发】十五、实操Linux开发中的中断,编写第一个按键驱动程序
  20. [博学谷学习记录]超强总结,用心分享|架构 Nacos入门

热门文章

  1. 2.8.1 矩阵的合同
  2. 通过wifi对手机进行adb连接debug调试 adb connect ip_address:5555
  3. 交叉编译详解 - 概念篇
  4. 利用Matlab编写龙贝格算法(romberg)求函数积分
  5. 加勒比海盗1英文剧本
  6. Echarts 多个网格联动坐标轴指示器(axisPointer)
  7. 弘辽科技:B站携手阿里入股如涵 UP主里要诞生下一个李佳琦?
  8. 传奇服务器端如何修改称号,图文解说传奇人物称号设置详细步骤
  9. springboot微软文本转语音(texttospeach) java实现微软文本转语音
  10. format函数用法详解