09-11-周四_16-52-32
This commit is contained in:
290
project03/01.python流程控制.md
Normal file
290
project03/01.python流程控制.md
Normal 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("你输了!😢")
|
||||
```
|
||||
|
BIN
project03/01.python流程控制/image-20240904142740373.png
Normal file
BIN
project03/01.python流程控制/image-20240904142740373.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
109
project03/03.[任务书]流程控制练习.md
Normal file
109
project03/03.[任务书]流程控制练习.md
Normal file
@@ -0,0 +1,109 @@
|
||||
## 猜数字小游戏
|
||||
|
||||
## 需求
|
||||
|
||||
- 编写一个猜数字游戏,随机产生1~100之间的整数,然后让用户输入猜的数字是什么
|
||||
- 每次用户输入数字之后是错误的,都会进行提示,输入的数字偏大还是偏小
|
||||
- 用户输入错误3次之后提示游戏结束,并且退出程序
|
||||
|
||||
## 参考代码
|
||||
|
||||
```python
|
||||
import random
|
||||
|
||||
# 产生随机数
|
||||
num = random.randint(1, 100)
|
||||
|
||||
# 定义可以猜的次数
|
||||
i = 3
|
||||
|
||||
# 开启循环,让用户输入数字,并且进行判断
|
||||
while i:
|
||||
i -= 1
|
||||
guess = input("输入1~100之间的整数:")
|
||||
# 判断用户输入的时候是纯数字
|
||||
if guess.isnumeric():
|
||||
# 纯数字就可以进行类型转换
|
||||
guess = int(guess)
|
||||
else:
|
||||
print("你输入的不是一个数字!")
|
||||
continue
|
||||
if(guess > num):
|
||||
print(f"你猜大了,还有{i}次机会")
|
||||
elif(guess < num):
|
||||
print(f"你猜小了,还有{i}次机会")
|
||||
else:
|
||||
print("你猜对了")
|
||||
break
|
||||
print("游戏结束")
|
||||
```
|
||||
|
||||
|
||||
|
||||
> <span style="color: red; background: yellow; padding: 2px 5px; font-size: 22px;">作业3.1提交的内容</span>
|
||||
>
|
||||
> - 程序运行成功的截图,单独发送给组长
|
||||
|
||||
## 完善石头剪刀布游戏
|
||||
|
||||
- 石头剪刀布游戏,让用户和计算机猜拳
|
||||
- 实现三局两胜
|
||||
|
||||
## 参考代码
|
||||
|
||||
```python
|
||||
import random
|
||||
|
||||
# 可选择的选项
|
||||
options = ["石头", "剪子", "布"]
|
||||
|
||||
# 初始化得分
|
||||
player_score = 0
|
||||
computer_score = 0
|
||||
win_required = 2 # 需要赢得的比赛局数
|
||||
|
||||
print("欢迎来到石头剪子布游戏!")
|
||||
print("三局两胜制。")
|
||||
print("请从以下选项中选择:")
|
||||
for i, option in enumerate(options):
|
||||
print(f"{i + 1}. {option}")
|
||||
|
||||
# 游戏主循环
|
||||
while player_score < win_required and computer_score < win_required:
|
||||
# 玩家选择
|
||||
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("你赢了!🎉")
|
||||
player_score += 1
|
||||
else:
|
||||
print("你输了!😢")
|
||||
computer_score += 1
|
||||
|
||||
# 检查是否已有胜者
|
||||
if player_score == win_required:
|
||||
print(f"恭喜你!你以 {player_score} 比 {computer_score} 赢得了比赛。")
|
||||
break
|
||||
elif computer_score == win_required:
|
||||
print(f"很遗憾,计算机以 {computer_score} 比 {player_score} 赢得了比赛。")
|
||||
break
|
||||
|
||||
# 游戏结束
|
||||
print("游戏结束!感谢参与。")
|
||||
```
|
||||
|
||||
|
||||
|
||||
> <span style="color: red; background: yellow; padding: 2px 5px; font-size: 22px;">作业3.2提交的内容</span>
|
||||
>
|
||||
> - 程序运行成功的截图,单独发送给组长
|
Reference in New Issue
Block a user