09-11-周四_16-52-32

This commit is contained in:
2025-09-11 16:52:33 +08:00
parent 944e303c7b
commit 658606c951
52 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,290 @@
# 流程控制
## 判断语句(if)
**语法:**
```python
if 条件:
满足条件后要执行的代码
```
<img src="./01.python流程控制/image-20240904142740373.png" alt="image-20240904142740373" style="zoom:80%;" />
### 单分支判断
```python
# 提示用户输入年龄
age = int(input("请输入你的年龄:"))
# 单分支判断
if age >= 18:
print("已经成年,可以去网吧上网了")
print("欢迎来到Eagles网吧")
print("你还没成年,要好好学习")
```
### 双分支判断
```python
"""
if 条件:
满足条件执行代码
else:
if条件不满足就走这段
"""
# 提示用户输入年龄
age = int(input("请输入你的年龄:"))
# 单分支判断
if age >= 18:
print("已经成年,可以去网吧上网了")
print("欢迎来到Eagles网吧")
else:
print("你还没成年,要好好学习")
```
### 多分支判断
```python
if 条件:
满足条件执行代码
elif 条件:
上面的条件不满足就走这个
elif 条件:
上面的条件不满足就走这个
elif 条件:
上面的条件不满足就走这个
else:
上面所有的条件不满足就走这段
# 成绩判断程序
# 提示用户输入成绩
score = int(input("请输入成绩0-100"))
# 多分支判断成绩
if score >= 90 and score <= 100:
print("优秀")
elif score >= 60 and score < 90:
print("良好")
elif score >= 0 and score < 60:
print("不及格")
else:
print("输入错误请输入0到100之间的成绩。")
```
## 循环语句-while
**语法:**
```python
while 条件:
循环体
# 循环条件可以直接是True/False或者1/0也可以是某个语句...
```
如果条件为真,那么循环体则执行
如果条件为假,那么循环体不执行
**示例:猜数字小游戏**
```python
print('猜数字游戏开始')
num = 54
while True:
guess = int(input("您猜数字是什么?(输入0-100的数字):"))
if guess < num:
print("您猜小了")
continue
elif guess > num:
print("您猜大了")
continue
break
print("您猜对了!")
# Output:
猜数字游戏开始
您猜数字是什么(输入0-100的数字):10
您猜小了
您猜数字是什么(输入0-100的数字):50
您猜小了
您猜数字是什么(输入0-100的数字):60
您猜大了
您猜数字是什么(输入0-100的数字):54
您猜对了
```
### 循环终止语句
#### break
用于完全结束一个循环,跳出循环体执行循环后面的语句
#### continue
和 break 有点类似,区别在于 continue 只是终止本次循环接着还执行后面的循环break 则完全终止循环
#### while...else结构
while 后面的 else 作用是指,当 while 循环正常执行完,中间没有被 break 中止的话,就会执行 else 后面的语句
- **语法:**
```python
while condition:
# 循环体
if some_condition:
break
else:
# 循环正常结束时执行的代码
```
- **示例:**
```python
# 寻找素数的示例
num = 10
i = 2
while i < num:
if num % i == 0:
print(f"{num} 不是素数,因为它可以被 {i} 整除。")
break
i += 1
else:
print(f"{num} 是一个素数!")
```
## 循环语句-for
for循环用户按照顺序循环可迭代对象的内容
**语法:**
```python
for variable in iterable:
# 循环体
# 执行的代码
```
### 遍历列表
```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Output:
apple
banana
cherry
```
### 遍历字符串
```python
word = "helloworld"
for letter in word:
print(letter)
# Output:
h
e
l
l
o
w
o
r
l
d
```
## enumerate
enumerate枚举对于一个可迭代的iterable/可遍历的对象如列表、字符串enumerate将其组成一个索引序列利用它可以同时获得索引和值。
```python
li = ['甲','乙','丙','丁']
for i in enumerate(li):
print(i)
for index,value in enumerate(li):
print(index,value)
for index,value in enumerate(li,100): #从哪个数字开始索引
print(index,value)
# Output:
(0, '甲')
(1, '乙')
(2, '丙')
(3, '丁')
0
1
2
3
100
101
102
103
```
## range
指定范围,生成指定数字
```bash
for i in range(1,10):
print(i)
for i in range(1,10,2): # 步长
print(i)
for i in range(10,1,-2): # 反向步长
print(i)
```
### 小游戏案例
石头简单布
```bash
import random
# random产生随机值或者从给定值中随机选择
# 可选择的选项
options = ["石头", "剪子", "布"]
print("欢迎来到石头剪子布游戏!")
print("请从以下选项中选择:")
for i, option in enumerate(options):
print(f"{i + 1}. {option}")
# 玩家选择
player_choice = int(input("请输入你的选择1-3")) - 1
# 计算机随机选择
computer_choice = random.randint(0, 2)
print(f"\n你选择了{options[player_choice]}")
print(f"计算机选择了:{options[computer_choice]}")
# 判断胜负
if player_choice == computer_choice:
print("平局!")
elif (player_choice == 0 and computer_choice == 1) or \
(player_choice == 1 and computer_choice == 2) or \
(player_choice == 2 and computer_choice == 0):
print("你赢了!🎉")
else:
print("你输了!😢")
```