判斷語句和循環語句
if-else
想?想:在使?if的時候,它只能做到滿?條件時要做的事情。那萬? 需要在不滿?條件的時候,做某些事,該怎么辦呢?
答:else
<1>if-else的使?格式
if條件:
??????滿?條件時要做的事情1
??????滿?條件時要做的事情2
??????滿?條件時要做的事情3
??????...(省略)...
else:
??????不滿?條件時要做的事情1
??????不滿?條件時要做的事情2
??????不滿?條件時要做的事情3
??????...(省略)...
demo1
chePiao = 1 #?1代表有?票,0代表沒有?票
if chePiao == 1:
print("有?票,可以上??")
print("終于可以?到Ta了,美滋滋~~~")
else:
print("沒有?票,不能上?")
print("親愛的,那就下次?了,?票難求啊~~~~(>_<)~~~~")
結果:有車票的情況
有?票,可以上??
終于可以?到Ta了,美滋滋~~~
結果2:沒有?票的情況
沒有?票,不能上課
親愛的,那就下次?了,?票難求啊~~~(>_<)~~~
<2>練?練
要求:從鍵盤輸???的?度,如果???度沒有超過10cm,則允許上? ?,否則不允許上??
elif
- 想一想
if能完成當xxx時做事情
if-else能完成當xxx時做事情1,否則做事情2
如果有這樣?種情況:當xxx1時做事情1,當xxx2時做事情2,當 xxx3時做事情3,那該怎么實現呢?
- 答:
elif
<1>elif的功能
elif的使?格式如下:
if xxx1:
??????事情1
elif xxx2:
??????事情2
elif xxx3:
??????事情3
說明:
- 當xxx1滿?時,執?事情1,然后整個if結束
- 當xxx1不滿?時,那么判斷xxx2,如果xxx2滿?,則執?事情2,然后 整個if結束
- 當xxx1不滿?時,xxx2也不滿?,如果xxx3滿?,則執?事情3,然后 整個if結束
demo:
score = 77
if score>=90 and score<=100:
print('本次考試,等級為A')
elif score>=80 and score<90:
print('本次考試,等級為B')
elif score>=70 and score<80:
print('本次考試,等級為C')
elif score>=60 and score<70:
print('本次考試,等級為D')
elif score>=0 and score<60:
print('本次考試,等級為E')
<2>注意點
- 1??可以和else?起使?
if 性別為男性:
??????輸出男性的特征
??????...
elif 性別為?性:
??????輸出?性的特征
??????...
else:
??????第三種性別的特征
??????...
說明:
- 當 “性別為男性”滿?時,執?“輸出男性的特征”的相關代碼
- 當 “性別為男性”不滿?時,如果“性別為?性”滿?,則執?“輸出?性的特征”的相關代碼
- 當 “性別為男性”不滿?,“性別為?性”也不滿?,那么久默認執? else后?的代碼,即 “第三種性別的特征”相關代碼
- 2??elif必須和if?起使?,否則出錯
if嵌套
通過學習if的基本?法,已經知道了
- 當需要滿?條件去做事情的這種情況需要使?if
- 當滿?條件時做事情A,不滿?條件做事情B的這種情況使?if-else
想?想:
坐??或者地鐵的實際情況是:先進?安檢如果安檢通過才會判斷是否有?票,或者是先檢查是否有?票之后才會進?安檢,即實際的情況某 個判斷是再另外?個判斷成?的基礎上進?的,這樣的情況該怎樣解決呢?
答:
if嵌套
<1>if嵌套的格式
if 條件1:
??????滿?條件1 做的事情1
??????滿?條件1 做的事情2
??????...(省略)...
??????if 條件2:
????????????滿?條件2 做的事情1
????????????滿?條件2 做的事情2
- 說明
1??外層的if判斷,也可以是if-else
2??內層的if判斷,也可以是if-else
3??根據實際開發的情況,進?選擇
<2>if嵌套的應?
demo:
chePiao = 1 # ?1代表有?票,0代表沒有?票
daoLenght = 9 # ??的?度,單位為cm
if chePiao == 1:
print("有?票,可以進站")
if daoLenght < 10:
print("通過安檢")
print("終于可以?到Ta了,美滋滋~~~")
else:
print("沒有通過安檢")
print("??的?度超過規定,等待警察處理...")
else:
print("沒有?票,不能進站")
print("親愛的,那就下次?了,?票難求啊~~~~(>_<)~~~~")
結果1:chePiao = 1;daoLenght = 9
有?票,可以進站
通過安檢
終于可以?到Ta了,美滋滋~~~
結果2:chePiao = 1;daoLenght = 20
有?票,可以進站
沒有通過安檢
??的?度超過規定,等待警察處理...
結果3:chePiao = 0;daoLenght = 9
沒有?票,不能進站
親愛的,那就下次?了,?票難求啊~~~(>_<)~~~
結果4:chePiao = 0;daoLenght = 20
沒有?票,不能進站
親愛的,那就下次?了,?票難求啊~~~(>_<)~~~
想?想:為什么結果3和結果4相同???
<3>練?練
情節描述:上公交?,并且可以有座位坐下
要求:輸?公交卡當前的余額,只要超過2元,就可以上公交?;如果空座位 的數量?于0,就可以坐下
應?:猜拳游戲
<1>運?效果:
<2>參考代碼:
import random
player = input('請輸?:剪?(0) ?頭(1) 布(2):')
player = int(player)
computer = random.randint(0,2)
# ?來進?測試
#print('player=%d,computer=%d',(player,computer))
if ((player == 0) and (computer == 2)) or ((player ==1) and (computer ==
print('獲勝,哈哈,你太厲害了')
elif player == computer:
print('平局,要不再來?局')
else:
print('輸了,不要?,洗洗?接著來,決戰到天亮')
循環介紹
<1>?活中的循環場景
跑道
?扇
CF加特林
<2>軟件開發中循環的使?場景
跟媳婦承認錯誤,說?萬遍"媳婦?,我錯了"
print("媳婦?,我錯了")
print("媳婦?,我錯了")
print("媳婦?,我錯了")
...(還有99997遍)...
使?循環語句?句話搞定
i = 0
while i<10000:
print("媳婦?,我錯了")
i+=1
<3>?總結
- ?般情況下,需要多次重復執?的代碼,都可以?循環的?式來完成
- 循環不是必須要使?的,但是為了提?代碼的重復使?率,所以有經驗的開發者都會采?循環
while循環
<1>while循環的格式
while 條件:
??????條件滿?時,做的事情1
??????條件滿?時,做的事情2
??????條件滿?時,做的事情3
??????...(省略)...
demo
i = 0
while i<5:
print("當前是第%d次執?循環"%(i+1))
print("i=%d"%i)
i+=1
結果:
當前是第1次執?循環
i=0
當前是第2次執?循環
i=1
當前是第3次執?循環
i=2
當前是第4次執?循環
i=3
當前是第5次執?循環
i=4
while循環應?
1. 計算1~100的累積和(包含1和100)
參考代碼如下:
#encoding=utf-8
i = 1
sum = 0
while i<=100:
sum = sum + i
i += 1
print("1~100的累積和為:%d"%sum)
2. 計算1~100之間偶數的累積和(包含1和100)
參考代碼如下:
#encoding=utf-8
i = 1
sum = 0
while i<=100:
if i%2 == 0:
sum = sum + i
i+=1
print("1~100的累積和為:%d"%sum)
while循環嵌套
- 前?學習過if的嵌套了,想?想if嵌套是什么樣?的?
- 類似if的嵌套,while嵌套就是:while??還有while
<1>while嵌套的格式
while 條件1:
??????條件1滿?時,做的事情1
??????條件1滿?時,做的事情2
??????條件1滿?時,做的事情3
??????...(省略)...
??????while 條件2:
????????????條件2滿?時,做的事情1
????????????條件2滿?時,做的事情2
????????????條件2滿?時,做的事情3
????????????...(省略)...
<2>while嵌套應??
要求:打印如下圖形:
*
* *
* * *
* * * *
* * * * *
參考代碼:
i = 1
while i<=5:
j = 1
while j<=i:
print("* ",end='')
j+=1
print("\n")
i+=1
<3>while嵌套應??:九九乘法表
參考代碼:
i = 1
while i<=9:
j=1
while j<=i:
print("%d*%d=%-2d "%(j,i,i*j),end='')
j+=1
print('\n')
i+=1
for循環
像while循環?樣,for可以完成循環的功能。
在Python中 for循環可以遍歷任何序列的項?,如?個列表或者?個字符串
等。
for循環的格式
for 臨時變量 in 列表或者字符串等:
??????循環滿?條件時執?的代碼
else:
??????循環不滿?條件時執?的代碼
demo1
name = 'dongGe'
for x in name:
print(x)
運?結果如下:
demo2
name = ''
for x in name:
print(x)
else:
print("沒有數據")
運?結果如下:
break和continue
1. break
<1> for循環
- 普通的循環示例如下:
name = 'dongGe'
for x in name:
print('----')
print(x)
執行結果:
----
d
----
o
----
n
----
g
----
G
----
e
- 帶有break的循環示例如下:
name = 'dongGe'
for x in name:
print('----')
if x == 'g':
break
print(x)
運行結果:
----
d
----
o
----
n
<2> while循環
- 普通的循環示例如下:
i = 0
while i<10:
i = i+1
print('----')
print(i)
執行結果:
----
1
----
2
----
3
----
4
----
5
----
6
----
7
----
8
----
9
----
10
- 帶有break的循環示例如下:
i = 0
while i<10:
i = i+1
print('----')
if i==5:
break
print(i)
運行結果:
----
1
----
2
----
3
----
4
----
小總結:
- break的作?:?來結束整個循環
2. continue
<1> for循環
- 帶有continue的循環示例如下:、
name = 'dongGe'
for x in name:
print('----')
if x == 'g':
continue
print(x)
運行結果:
----
d
----
o
----
n
----
----
G
----
e
<2> while循環
- 帶有continue的循環示例如下:
i = 0
while i<10:
i = i+1
print('----')
if i==5:
continue
print(i)
運行結果:
----
1
----
2
----
3
----
4
----
----
6
----
7
----
8
----
9
----
10
小總結:
- continue的作?:?來結束本次循環,緊接著執?下?次的循環
3. 注意點
- break/continue只能?在循環中,除此以外不能單獨使?
- break/continue在嵌套循環中,只對最近的?層循環起作?
總結
- if往往?來對條件是否滿?進?判斷
- if有4中基本的使??法:
1??基本方法
if 條件:
??????滿?時做的事情
2??滿?與否執?不同的事情
if 條件:
??????滿?時做的事情
else:
??????不滿?時做的事情
3??多個條件的判斷
if 條件:
??????滿?時做的事情
elif 條件2:
??????滿?條件2時做的事情
elif 條件3:
??????滿?條件3時做的事情
else:
??????條件都不滿?時做的事情
4??嵌套
if 條件:
??????滿?時做的事情
??????這?還可以放?其他任何形式的if判斷語句
- while循環?般通過數值是否滿?來確定循環的條件
i = 0
while i<10:
print("hello")
i+=1
- for循環?般是對能保存多個數據的變量,進?遍歷
name = 'dongGe'
for x in name:
print(x)
- if、while、for等其他語句可以隨意組合,這樣往往就完成了復雜的功能
作業
1. 使?if,編寫程序,實現以下功能:
- 從鍵盤獲取?戶名、密碼
- 如果?戶名和密碼都正確(預先設定?個?戶名和密碼),那么就顯示“歡迎進?xxx的世界”,否則提示密碼或者?戶名錯誤
username = input("請輸入用戶名:")
password = input("請輸入密碼:")
if username == 'root' and password == '123456':
print("歡迎進?xxx的世界")
else:
print("用戶名或密碼錯誤")
2. 使?while,完成以下圖形的輸出
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
i = 1
while i <= 5:
j = 1
while j<=i:
print("* ", end='')
j += 1
print("\n")
i += 1
m = 1
while m < 5:
n = 1
while n < 5:
if n >= m:
print("* ", end='')
n += 1
print("\n")
m += 1
倒三角形的打印可以用for循環
for i in range(1, 5):
for j in range(1, 5):
if j>=i:
print("* ", end='')
print("\n")