# 条件判断与循环

• 条件判断
• while循环
• for循环
• for循环嵌套

### 条件判断

if 条件表达式 :

elif 条件表达式 :

elif 条件表达式 :

``````"""
if 条件1：
条件1为真的情况下，执行的代码(会干的事情)
elif 条件2:
条件2为真的情况下，执行的代码(会干的事情)
else:
条件1和条件2都不为真的情况下，执行的代码(会干的事情)
"""

score = int(input('请输入十七的数学成绩：'))
if score >= 80:
print('奖励一台新电脑')
elif score >= 60:
print('奖励一顿油焖虾')
else:
print('奖励一顿胖揍')
# 条件1：	# 请输入刘星的数学成绩：90
# 奖励一台新电脑

# 条件2： 	# 请输入刘星的数学成绩：75
# 奖励一顿油焖虾

# 条件3：	# 请输入十七的数学成绩：55
# 奖励一顿胖揍``````

### while循环

`````` while 条件表达式 :
代码块
else：
代码块
``````

while 条件:

while的特点：

``````i = 1
while i < 5 :
print(i)
i = i + 1

1
2
3
4``````

### for循环

for循环 - for遍历 - 列表/字典/元组/字符串

``````ch_num = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']
for i in ch_num:
print(i)

for循环 - for遍历 - 列表/字典/元组/字符串

``````字典的遍历：

1、遍历key
for key in dict.keys()

2、遍历key-value
for item in person_info.items():
print(item)

for key,value in person_info.items():
print(key,value)``````

``````dict_01 = {
"name": "小简", "age":18, "city": "长沙"}
for x,y in dict_01.items():
print(f"查看key的信息：{x}和value信息{y}")

### for循环嵌套

``````for 变量 in 循环规则:
for 变量 in 循环规则:
代码块
代码块
``````
``````for x in range(1, 10):
for y in range(1, x + 1):
print(f"{x} * {y} = {x * y}", end=" ")
print(" ")

1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81``````