Python学习日志Day2

一、if-else语句

1.单分支if-else

1
2
3
4
5
6
7
score = 60
if score >= 60:
print('成绩及格')
pass
else:
print('成绩不及格')
pass

2.多分支elif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
score = 75
if score >= 90:
print('您的成绩是A')
pass
elif score >= 80:
print('您的成绩是B')
pass
elif score >= 70:
print('您的成绩是C')
pass
elif score >= 60:
print('您的成绩是D')
pass
else:
print('您的成绩是E')
pass

3.多分支if-else嵌套


二、while循环

1.猜拳机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#猜拳机
import random
i = 1
while i <= 3:
person = int(input('请出拳:[0:石头、1:剪刀、2:布]:'))
computer = random.randint(0, 2)
if person == 2 and computer == 0:
print('胜利!')
pass
elif person == 1 and computer == 2:
print('胜利!')
pass
elif person == 0 and computer == 1:
print('胜利!')
pass
elif person == computer:
print('平局')
pass
else:
print('失败~')
pass
i += 1
pass

三、for循环

1.遍历容器

1
2
3
for data in range(1, 101):
print(data, end = ' ')
pass

四、break、continue语句

1.break和continue语句必须在循环当中使用

2.continue结束当前循环并进入下一个循环

3.break结束整个循环不再继续(while-else结构、for-else结构也是如此,不再执行else)