Learn Python The Hard Way学习(30) - Else和If
来源:互联网 发布:unity3d导出fbx插件 编辑:程序博客网 时间:2024/06/10 05:45
下面我们给出上一节的加分练习的答案:
1. if语句下面的代码是if的一个分支。就像书里的一个章节,你选择了这章就会跳到这里阅读。这个if语句就像是说:“如果布尔判断为True,就执行下面的代码,否则跳过这些代码”。
2. 用冒号结束一个语句就是要告诉python,我要开始一个新的代码段了。缩进4个空格就是说,这些代码是包含在这个代码段中的,和函数的使用一样。
3. 不缩进会报错,python规定冒号后面语句必须有缩进。
4. 可以,而且可以是复杂的语句。
5. 修改变量的值后,判断语句就会相应的变True或者False,然后输出不同的语句。
比较我的答案和你自己的答案,确保你能理解代码块这个概念,因为这个对于下面的练习非常重要。
输入下面的代码,运行它:
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 dicide."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."
运行结果
root@he-desktop:~/mystuff# python ex30.py
We should take the cars.
Maybe we could take the buses.
Alright, let's just take the buses.
加分练习
1. 猜测一下elif和else是做什么的?
2. 改变变量的值,看程序输出有什么不同。
3. 尝试一下复杂的布尔表达式。
4. 给每行都添加注释。
- Learn Python The Hard Way学习(30) - Else和If
- Learn Python the hard way. 习题30. If和else/elif
- Learn Python The Hard Way学习(29) - 什么是If
- Learn Python The Hard Way学习(2) - 注释和#号
- Learn Python The Hard Way学习(4) - 变量和命名
- Learn Python The Hard Way学习(14) - 提示和传递
- Learn Python The Hard Way学习(19) - 函数和变量
- Learn Python The Hard Way学习(32) - 循环和列表
- Learn Python The Hard Way学习(35) - 分支和函数
- Learn Python The Hard Way学习(36) - 设计和调试
- 《Learn Python the hard way》学习记录
- 【Python】Learn Python the hard way, ex29 if语句
- Learn Python The Hard Way-Ecercise26~30
- Learn Python The Hard Way
- Learn Python The Hard Way
- 学习笔记--python(1)--learn python the hard way
- 【Python】Learn Python the hard way, ex0 学习使用Terminal
- Learn Python The Hard Way学习(41) - 学习面向对象
- android bundle 使用注意
- java递归算法实现
- 实现矩阵对角线输出
- SQL Server 触发器入门详解
- 将文件转换成二进制,再由二进制转换成文件
- Learn Python The Hard Way学习(30) - Else和If
- ALSA各文件作用
- applicationContext.xml
- iOS应用突然间无法启动的怪异问题 LOG:A valid provisioning profile for this executable was not found
- 架构之美第四章-架构与美
- XCODE 4.3.1 调试IOS3的PHONE
- 转 parted创建GPT分区
- Get和Set Cookie
- dwr.xml