一席怪,if 判斷
---------示例1
# 單分支
age=25
salary=88
height=180
if age<28 and salary > 50 and height == 180:
print('hello,lets dinner together')
# 雙分支
age=25
salary=88
height=180
if age<28 and salary > 50 and height == 180:
print('hello,lets dinner together')
else:
print('ugly man')
---------示例2
# 多分支
score=input("填入分?jǐn)?shù):")
score=int(score)
if score >= 90:
print("優(yōu)秀")
if score > 100:
print("滿分哦")
else:
print("距離滿分差一乃乃")
elif score >=80:
print("良好")
else:
print("差")
二, while 循環(huán)
1) 基本用法
i=1
while i < 5:
print(i)
i+=1
1
2
3
4
2) 死循環(huán)
# 1)不會(huì)讓cpu使用率飆升
while True:
input('name:')
# 2)會(huì)讓cpu使用率飆升
while True:
1 + 1
3) 跳出/結(jié)束循環(huán)
1畜普,條件為假
方式一:
#例1:
i=1
tag=True
while tag:
if i ==4:
tag=False
print(i)
i+=1
print('end')
1
2
3
4
end
# 例2:
i=1
tag=True
while tag:
print(i)
i+=1
if i ==4:
tag=False
print('end')
1
2
3
end
2,break
# (直接終止本層循環(huán)护侮,根本沒(méi)有下一次)
#例1:
i=1
while True:
if i == 4:
break
print(i)
i+=1
1
2
3
#例2:
i=1
while True:
print(i)
i+=1
if i == 4:
break
PS:
結(jié)果一樣敌完,第二種更好,只需要循環(huán)三次
第一種循環(huán)4次才結(jié)束
PS:
結(jié)束while循環(huán)的2種方式
tag=True
while tag:
while tag:
while tag:
tag=False
while True:
while True:
while True:
break
break
break
3羊初,continue
#示例:
跳過(guò)4
i=1
while i <= 5:
if i == 4:
i += 1
continue
print(i)
i+=1
1
2
3
5
注意:
# continue同一級(jí)別的后面不要再寫代碼
# 循環(huán)體代碼最后一步不要寫continue
4滨溉,while+ else
i=1
while i <= 5:
print(i)
i+=1
else:
print('滿足循環(huán)正常非break運(yùn)行')
1
2
3
4
5
滿足循環(huán)正常非break運(yùn)行
============
i=1
while i <= 5:
if i == 4:
break
print(i)
i+=1
else:
print('滿足循環(huán)正常非break運(yùn)行')
1
2
3
5, 用戶登錄驗(yàn)證长赞,三次密碼錯(cuò)誤登陸退出
# 方式一
count=0
while True:
name=input('name:')
password=input('password:')
if name == 'mz' and password == '123':
print('login succeed')
break
else:
print('faild login')
count+=1
if count == 3:
print('too many login,exiting')
break
name:zx
password:zx
faild login
name:zx
password:zx
faild login
name:zx
password:zx
faild login
too many login,exiting
name:mz
password:123
login succeed
======================
count=0
while True:
name=input('name:')
password=input('password:')
if name == 'mz' and password == '123':
print('login succeed')
while True:
print('輸入操作編號(hào)')
choice=input('>>>')
if choice == '0':
break
elif choice == '1':
print('取款')
elif choice =='2':
print('轉(zhuǎn)賬')
break
else:
print('faild login')
count+=1
if count == 3:
print('too many login,exiting')
break
name:mz
password:123
login succeed
輸入操作編號(hào)
>>>1
取款
輸入操作編號(hào)
>>>2
轉(zhuǎn)賬
輸入操作編號(hào)
>>>0
注意:
1)if name == 'mz' and password == '123': 要加引號(hào)
2)if choice == '0': 數(shù)字要加引號(hào)
3)if choice == '0':
break 連個(gè)break退出循環(huán)晦攒,對(duì)應(yīng)兩個(gè)while,位置不能放錯(cuò)
# 方式二
count=0
tag=True
name=input('name:')
passwd=input('passwd:')
while tag:
if name == 'mz' and passwd == '123':
print('login succeed')
while tag:
print('''
輸入編碼
0 退出
1 查詢
2 轉(zhuǎn)賬
''')
choice=input('choice:')
if choice == '0':
tag = False
elif choice == '1':
print('查詢')
elif choice == '2':
print('轉(zhuǎn)賬')
else:
print('輸入錯(cuò)誤得哆,重新輸入')
else:
print('faild to login')
count+=1
if count == 3:
print('too many times')
break
注意:
tag = True
使用變量作為條件脯颜,把條件改為假,只需要一次
tag = False
四贩据,for循環(huán)
l=[11,22,33,44]
i=0
while i <len(l):
print(l[i])
i+=1
等同于
for x in l:
print(x)
11
22
33
44
======================
dic={"name":"mz","age":88}
for i in dic:
print(i)
name
age
dic={"name":"mz","age":88}
for i in dic:
print(i,dic[i])
name mz
age 88
======================
info=[["name","mz"],["age",22]]
for i in info:
print(i)
info=[["name","mz"],["age",22]]
for x,y in info:
print(x,y)
['name', 'mz']
['age', 22]
name mz
age 22
五栋操,range范圍
1)使用
i=0
while True:
if i < 3:
print('hello')
i+=1
等同于
l=[11,22,33]
for x in l:
print('hello')
等同于
for x in range(3):
print('hello')
hello
hello
hello
PS:
python3直接范圍取值(老母雞)
python2是下蛋闸餐,占內(nèi)存
2)for + break
# for + break
for x in range(5):
if x == 3:
break
print(x)
0
1
2
# for + continue
for i in range(5):
if i ==3:
continue
print(i)
0
1
2
4
#for + else
for i in range(5):
if i ==3:
continue
print(i)
else:
print('endendend')
0
1
2
4
endendend