逻辑 与 判断Logics and Conditions

概览

药水商店案例

对顾客增收 1元 塑料袋环保费

代码
1 2 3 4 5 6+7+8+9+10+11+12*13 14
price = float(input())amount = int(input()) total = price * amount print("要袋子么?")print("1 要")print("2 不要")option = int(input()) if option == 1: total += 1 print(total)
特别注意

缩进

缩进

代码中表达层级的东西

缩进 与 空格

一个缩进 差不多 等于 4个空格

操作
MacWindows
制造缩进tabTab
取消缩进⇧ shift + tabShift + Tab

条件控制

上面代码中

total += 1

只有在 option == 1 对的情况下才执行

布尔值

一种用来表达对错的机制

Ture / False

比较运算符

option == 1 会产出 布尔值,用于 if 语句的条件控制

比较运算符Equality and Ordering Operators

是什么

<   >   !=
<=  >=  ==

比较两个值 产出 布尔值

等价比较

正常情况
1 <2 <3 <4 <
print(1 == 1)True print(1.2 == 1.2)True print(1 == 1.0)True print("zzax" == "zzax")True
特殊情况
1 <
print(1 == "1")False

大小比较

正常情况
1 <2 <3 <
print(1 < 2)Trueprint(1.0 >= 2.0)Falseprint(1 < 2.3)False
需要注意

很多情况下,非数值类型 无法进行大小比较

1!
print(1 <= "12")

比较时会报错

布尔值Bool

布尔值字面量 与 类型

True 
False 
案例
1 2 <3 <4 <
exist = True print(exist)Tureprint(type(True))class 'bool'print(type(1 == 1))class 'bool'

布尔值 比较运算

1 <
print(True == True)class 'bool'

布尔值化

其它类型可以通过调用 bool(x) 将其转变为 bool 类型

1 <
print(bool(1))True
规律
0
0.0
""
[]
{}
()
None 

以上会转化为 False,其它转换为 True

目前只需要记忆 0 和 0.0 等价于 False 即可

if 语句if Statements

药水商店

1 2 3 4 5 6 7 8 9 10 11 12 13 14
price = float(input())amount = int(input()) total = price * amount print("要袋子么?")print("1 要")print("2 不要")option = int(input()) if option == 1: total += 1 print(total)

if 语句 基本认知

格式
if <condition>:
    <statement>
    ...
效果

if 语句管辖的内容,只有 condition 通过 才会执行。

if 语句 condition 的要求

正常情况

需要一个 bool 类型,作为判断依据。

1 2 <3 4 <
if True: print("haha")hahaif 1 < 2: print("1 < 2")1 < 2
需要注意

如果不是 bool 类型,会被自动 变为 bool 类型,作为判断依据。

变形规则 参考 布尔值化 的规则。

1 2 <
if 3: print("3 is True")3 is True

缩进Indentation

干什么用

代表从属关系

在 缩进内的,从属 if 语句管辖,

案例 1
1 2 3
if input() == "9917": print("密码正确") print("登录成功")
案例 2
1 2 3
if input() == "9917": print("密码正确")print("登录成功")
收管辖语句必须对齐

对不齐,或者有多余缩进会报错

1 2!3
if input() == "9917": print("密码正确") print("登录成功")

在其它语言中

缩进不是语法的一部分,但是是规范的一部分

1 2 3 4
if (Console.readInt() == 1) { Console.println("密码正确");Console.println("登录成功");}

缩进 与 if

有 缩进的 不一定执行

找控制的 L 大法

布尔运算符Boolean Operators

是什么

对 对与错 计算的运算符

有哪些

and or not
and 运算符

并且

都对才对

1 =2 3 =
v1 = True and TrueTrue v2 = True and FalseFalse
or 运算符

或者

一个对 就对

1 =2 3 =
v1 = False or FalseFalse v2 = True or FalseTrue
not 运算符

逻辑反转

1 =
v1 = not TrueFalse

与 比较运算符 组合 表达范围

1 2
if 10 < price and price < 100: ...
Python 独有的 简略写法
1*2
if 10 < price < 100: ...

断义

当有一个值能断定结果时, 后面的表达式不执行。

没有得出结果,会一直执行
1 2 3 4
min = 3max = 7x = 4result = min < x and x < max = 3 < x and x < max = 3 < 4 and x < max = True and x < max = True and 4 < max = True and 4 < 7 = True and True = True
能断定结果,立刻得出结果

and 中途得到 False 会立刻终止

1 2 3 4
min = 3max = 7x = 1result = min < x and x < max = 3 < x and x < max = 3 < 1 and x < max = False and x < max = False
or 同理

or 中途得到 True 会立刻终止

1 2 3 4
min = 3max = 7x = 1result = x < min or max < x = 1 < min or max < x = 1 < 3 or max < x = True or max < x = True

非 bool 类型参与运算

会被自动转为 bool 类型 进行判断

and 实质

返回 第一个 布尔值化后 是 False 的,

1 2 <
result = 0 and True print(result)0

或者 最后一个

1 2 <
result = 3 and 4print(result)4
or 实质

返回 第一个 布尔值化后 是 True 的,

1 2 <
result = 3 or 17print(result)3

或者 最后一个

1 2 <
result = 0 or 0.0print(result)0.0
与 if 语句结合
1 2 3
result = 3 and 4if result: print("True")
实际应用

使用该规则达到 默认值的效果

1 2 3
print("请输入玩家姓名")name = input() or "玩家1"print("你好, " + name)

优先级常备小括号

记不住的时候,加小括号,保险

不好
result = a and b or c
result = a and (b or c)

if 语句 常用版型

一个 if 语句

1 2 3 4
1if 2: 34

2 如果对 走

1 2 3 4

2 如果不对

1 2 4

两个嵌套的 if 语句

1 2 3 4 5 6 7
1if 2: 3 if 4: 5 67

只有 2 对了,才会 执行 3 - 6

如果 2 是错的,4 是对的,程序的执行序列是什么
1 2 7

两个并列 if 语句

1 2 3 4 5 6 7
1if 2: 34if 5: 67

两个 if 语句 互不影响

3 6 有没有可能 都 执行?
A

B

不会

3 6 有没有可能 都不 执行?
A

B

不会

如果 2 是错的,5 是对的,程序的执行序列是什么
1 2 4 5 6 7

一个 if 语句 带 else 分支

1 2 3 4 5 6
1if 2: 3else: 45

2 如果对 走

1 2 3 5

2 如果不对 走

1 2 4 5
3 4 有没有可能 都 执行?
A

B

不会

3 4 有没有可能 都不 执行?
A

B

不会

一个 if 语句 带 elif 分支

elif 就是 else if

1 2 3 4 5 6 7
1if 2: 3elif 4: 5}6

2 如果对 走

1 2 3 6

2 如果不对 4 如果对 走

1 2 4 5 6

2 4 都不对 走

1 2 4 6 
一个 if 语句 带 elif 分支 和 else 分支
1 2 3 4 5 6 7 8 9
1if 2: 3elif 4: 5else: 6}7
2 对 4 不对 会走哪
1 2 3 7
2 对 4 对 会走哪
1 2 3 7
2 不对 4 对 会走哪
1 2 4 5 7
2 不对 4 不对 会走哪
1 2 4 6 7

if 语句 完整规则

一个 if 语句中

if 只能出现 1 次

elif 可以出现 0 - n 次

else 可以出现 0 - 1 次

组合方式

可以并列

可以嵌套

语句 与 分支 对比

一个 if 语句 多个 branch

最多执行 1 个 branch

不可能执行 2 个以上的 branch

多个 if 语句,每个 1 个 branch

可能执行多个 branch

因为 这些 if 语句 互不影响

if 语句 逻辑关系

挑战 1

功能

输入 分数

输出 评分等级

数据
分数等级
0 - 59挂科
60 - 100通过
样例
> <
78通过

需求的逻辑范围

如果需求上指明,用户绝对不会输入非法范围内的值,则代码不需要考虑

使用前
1 2 3 4 5 6
score = 78if 0 <= score < 60: print("挂科") elif 60 <= score < 100: print("通过")
使用后
1 2*3 4 5*6
score = 78if score < 60: print("挂科") else: print("通过")

挑战 2

数据
分数等级
0 - 59挂科
60 - 79通过
80 - 100优秀
样例
> <
88优秀

if 语句逻辑关系

每个后面的 condition 只有在前面所有 condition 都不成立的情况下 才 check

只可能走到一个 branch 里

使用前
1 2 3 4 5 6 7 8 9
score = 88if score < 60: print("挂科") elif 60 <= score and score < 80: print("通过") else: print("优秀")
使用后
1 2 3 4 5*6 7 8 9
score = 88if score < 60: print("挂科") elif score < 80: print("通过") else: print("优秀")

if 语句 综合练习

挑战 1

结账 第一版本
程序让用户输入金额
< >
输入金额200
如果金额 不到 100 元

收取固定 6 元 物流费用

< < <
商品总价:30 元 物流费:6 元订单总价:36 元
如果金额 达到 100 元

就包邮处理

< < <
商品总价:160 元 物流费:免费订单总价:160 元

挑战 2

增加新规则

如果金额 达到 200 元

就 - 40

< < < <
商品总价:200 元 优惠:40 元物流费:免费订单总价:160 元

图格子的故事

参考答案

另外一个思路

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
price = int(input())print("商品总价:" + str(price) + " 元") total = price # 满 200 减 40if price >= 200: discount = 40 total = price - discount print("优惠:" + str(discount) + " 元") # 满 100 免 运送费if price >= 100: print("运送费:" + "免费")else: shipping_fee = 6 total = price + shipping_fee print("运送费:" + str(shipping_fee) + " 元") # 显示支付print("您需支付:" + str(total) + " 元")

人工内存

是什么

模拟计算机处理代码的过程

目标

没有运行代码,但已经在脑子里运行了这个代码

要求

你需要知道 计算机是 如何运行代码

你需要知道 在任何一个时间

都有哪些变量活着

并且 这些变量的值 都是多少

样例

代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
price = int(input())print("商品总价:" + str(price) + " 元") total = price # 满 200 减 40if price >= 200: discount = 40 total = price - discount print("优惠:" + str(discount) + " 元") # 满 100 免 运送费if price >= 100: print("运送费:" + "免费")else: shipping_fee = 6 total = price + shipping_fee print("运送费:" + str(shipping_fee) + " 元") # 显示支付print("您需支付:" + str(total) + " 元")
内存
Line                                io
1       97                          >   97
2       |                           <   总价:97 元
4       |       97 
7       |       |                   =   False
13      |       |                   =   False
16      |       |       6           
17      |       103     |           
18      |       |       |           <   运送费:6 元
21      |       |       |           <   您需支付:103 元 
        price   total   shipping_fee

程序调试Debugging

输出调试

检查点

在某些位置输出信息

运行后,在控制台检查是否显示,从而证明这一行有没有执行

1 2 3 4 5
...if ...: print("Check Point 1") ......
输出数据

在某些位置输出信息

运行后,在控制台检查是否显示,从而证明这一行有没有执行

1 2 3
a = 0a = ...print("a: " + str(a))

Debug 模式

增加断点

在行号 右边,可以点一下,会点出一个粉刺

就是断点

进入 debug 模式

文件 点击右键,出来的菜单里,点击 Debug

之后就可以进入 debug 模式

debug 模式

debug 模式下,程序运行到 断点处,就会暂停

控制面板

检查数据

小跳跃按钮 下一行

继续播放按钮 继续执行

红色方块按钮 结束执行

if 语句 变体版本

if 一行缩写

使用前
1 2 3
name = input()if not name: print("您没有输入任何名称")
特性

只有 一个 if,没有 else 也没有 elif

使用后
1 2
name = input()if not name: print("您没有输入任何名称")

if 表达式

使用前
1 2 3 4
if x < 0: a = -1else: a = 1
特性

if else

都是一个赋值语句

给同一个变量

使用后
1
a = -1 if x < 0 else 1
模板
<expression1> if <condition> else <expression2> 
样例
1
x *= -1 if x < 0 else 1

ZZAX 微信公众

文档一更新,立刻告诉你