一、if判断
if 条件:
code1
code2
空格
name = input('your name is :')
if name == 'Lunar':
print('hello!!!')
your name is : Lunar
hello!!!
if条件:
code1
else:
code2
猜年龄游戏
correct_age = 16
age = int(input('age is :'))
if age == correct_age :
print('猜对了!')
else:
print('不对!')
age is : 16
猜对了!
if条件:
code1
elif条件:
code2
else:
code3
correct_age = 16
age = int(input('age is :'))
if age == correct_age :
print('猜对了!')
elif age > correct_age:
print('我才没那么老!')
else:
print('猜小了!')
age is : 21
我才没那么老!
if的嵌套
if条件为True:
if条件为True:
if条件为True:
correct_age = 16
age = int(input('age is :'))
if age != correct_age :
if age > correct_age:
print('大了!')
else:
print('小了!')
else:
print('对了!')
age is : 12
小了!
二、while循环
while条件:
code1
while 1:
correct_age = 16
age = int(input('age is :'))
if age != correct_age :
if age > correct_age:
print('大了!')
else:
print('小了!')
else:
print('对了!')
break
age is : 12
小了!
age is : 21
大了!
age is : 16
对了!
break 终止循环 continue 跳过本次循环的剩余部分 pass 空操作
gift_info = {'1':'doll', '2':'flower', '3':' sparkling water', '4':'cookie'}
while 1:
correct_age = 16
age = int(input('age is :'))
if age == correct_age :
print('猜对了!选个奖励吧!\n',gift_info)
break
elif age > correct_age:
print('我才没那么老!')
else:
print('猜小了!')
while 1:
choice = int(input('你的选择是:'))
if choice == 1:
print('cute doll!')
break
if choice == 2:
print('这个不行!')
continue
if choice == 3:
print('nice sparkling water!')
break
if choice == 4:
print('delicate cookie!')
break
else:
print('???')
age is : 16
猜对了!选个奖励吧!
{'1': 'doll', '2': 'flower', '3': ' sparkling water', '4': 'cookie'}
你的选择是: 2
这个不行!
你的选择是: 5
???
你的选择是: 3
nice sparkling water!
count = 1
while count < 21:
if count != 10:
print(count)
count = count + 1
# 打印1~20,不打10
1
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
count = 1
while count < 21:
if count == 10:
count = count + 1
continue
print(count)
count = count + 1
# 打印1~20,不打10
1
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
三、for循环
food_list = ['蛋糕', '烤鱼', '汉堡', '焦糖布丁','冰激淋']
n = 0
while n < 5:
print(food_list[n])
n += 1
蛋糕
烤鱼
汉堡
焦糖布丁
冰激淋
for i in 列表/字典(取出key):
code1
food_list = ['蛋糕', '烤鱼', '汉堡', '焦糖布丁','冰激淋']
for i in food_list:
print(i)
蛋糕
烤鱼
汉堡
焦糖布丁
冰激淋
info = {'name': 'nick', 'age': 19}
for item in info:
# 取出info的keys
print(item)
name
age
循环 + else # 没有break就else
for i in range(3):
print(i)
else:
print('没有break')
0
1
2
没有break
import time
print ('loading' , end = '' )
count = 0
while count < 3:
count = count + 1
for i in range (3):
print ('.' , end = '')
time.sleep(1)
# end取消换行
loading.........
s=['h','e','l','l','o']
l=['']*len(s)
for i in range(len(s)) :
l [i]= s[len(s)-1-i]
print (l)
['o', 'l', 'l', 'e', 'h']
age = int(input('请输入你的年龄:'))
if age < 18 :
print('未成年')
elif age < 25:
print('表白')
elif age < 45:
print('阿姨好')
elif age > 45 :
print('奶奶好')
请输入你的年龄: 12
未成年
i = 1
while i < 101:
if i%2 == 0:
print(i)
i += 1
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100
correct_age = 27
while 1:
for i in range(3):
age = int(input('请猜测年龄'))
if age == correct_age:
print('恭喜你猜对了!')
break
else:
print('不对,再猜一次吧')
else:
choice = input('还想玩吗?')
if choice == 'Y' or choice == 'y':
continue
if choice == 'N' or choice == 'n':
break
break
请猜测年龄 13
不对,再猜一次吧
请猜测年龄 15
不对,再猜一次吧
请猜测年龄 17
不对,再猜一次吧
还想玩吗? y
请猜测年龄 169
不对,再猜一次吧
请猜测年龄 24
不对,再猜一次吧
请猜测年龄 27
恭喜你猜对了!
max_level = int(input('max level ='))
current_level = 1
while current_level < max_level + 1:
xing = current_level*2-1
kong = ((max_level*2-1)-xing)//2
level = ' ' * kong + '*'*xing + ' ' * kong
print(level)
current_level += 1
max level = 9
*
***
*****
*******
*********
***********
*************
***************
*****************
浙公网安备 33010602011771号