资源池
链接:https://pan.baidu.com/s/1OGq0GaVcAuYEk4F71v0RWw
提取码:h2sd
本章内容:
- if判断语句
- for循环语句
- while循环语句
- break,continue和pass
if判断语句
语法:
if 条件:
代码块
elif 条件:
代码块
else:
代码块
示例
a = 10
if a > 5:
print('大于5')
elif a > 9:
print('大于9')
...运行结果
大于5
for循环语句
语法:
for i in 可迭代对象
代码块
- 可迭代对象:字符串,列表,元组,字典,集合
示例:
info = {'name':'007','age':88}
for i in info.items():
print(i)
.........运行结果
('name', '007')
('age', 88)
for i in range(1,10,3):#range顾头不顾尾
print(i)
.............................
1
4
7
while循环语句
语法:
while 条件:
循环体
示例:0-10之间的偶数
count = 0
while count <= 10:
print(count)
count += 2
...运行结果
0
2
4
6
8
10
死循环
语法: while True --> 死循环
while True:
循环体
注:用break跳出循环
示例:0-10之间的偶数
count = 0
while True:
print(count)
count += 2
if count > 10:
break
break,continue和pass
- break 跳出循环,执行下一条命令;
- continue 跳出本次循环,进入下一次循环;
- pass 占位符
拓展:石头剪刀布游戏
import random
range=['石头','剪刀','布']
print('欢迎来到召唤死侠骨!今晚让我们寻出万年前的天选之子!')
grade={'胜':0,
'平':0,
'败':0
}
playtime=1
print('请根据你最真诚心召唤你的武器!')
while True:
print('ROUND ', playtime)
print('武器池:(石头,剪刀,布)','或call "2"退出')
player = input('OK!请输入你的武器:')
computer = random.choice(range)
#ying
if computer == '石头'and player=='布'or computer == '剪刀'and player=='石头'or computer == '布'and player == '剪刀':
print('胜')
grade['胜'] += 1
playtime+=1
print(grade)
print('='*100)
#shu
elif computer == '石头' and player == '剪刀' or computer == '剪刀' and player == '布' or computer == '布' and player == '石头':
print('败')
grade['败'] += 1
playtime += 1
print(grade)
print('=' * 100)
#ping
elif computer==player:
print('平')
grade['平'] += 1
playtime += 1
print(grade)
print('=' * 100)
#t退出
elif player=='2':
print('你就逆天改命沉睡万年的天选之子,今天你战胜了自己!!!')
print(grade)
break
#其他情况
else:
print('mader fuck!!蠢材!!')
break