第四章,了不起的分支和循環
?
4.1 分支和循環
Python主要依靠縮進來區分代碼塊
?
4.2 快速上手
成績按照分數來劃分等級,90分以上為A,80~90 為B,60~80 為C,60以下為D
p4_1.py
score = int(input(
"
請輸入一個分數:
"
))
if
100 >= score >= 90
:
print
(
"
A
"
)
if
90 > score >= 80
:
print
(
"
B
"
)
if
80 > score >= 60
:
print
(
"
C
"
)
if
60 > score >=
0:
print
(
"
D
"
)
if
score < 0
or
score > 100
:
print
(
"
輸入錯誤!
"
)
p4_2.py
score = int(input(
"
請輸入您的分數:
"
))
if
100 >= score >= 90
:
print
(
"
A
"
)
else
:
if
90 > score >= 80
:
print
(
"
B
"
)
else
:
if
80 > score >= 60
:
print
(
"
C
"
)
else
:
if
60 > score >=
0:
print
(
"
D
"
)
else
:
print
(
"
輸入錯誤!
"
)
p4_3.py
score = int(input(
"
請輸入您的分數:
"
))
if
100 >= score >= 90
:
print
(
"
A
"
)
elif
90 > score >= 80
:
print
(
"
B
"
)
elif
80 > score >= 60
:
print
(
"
C
"
)
elif
60 > score >=
0:
print
(
"
D
"
)
else
:
print
(
"
輸入錯誤!
"
)
?
分析:
p4_1.py中,假設輸入的是98,程序第一次成立,打印A,不過程序還為結束,需要繼續對后面的四個條件進行判斷,直到所有的條件不符合,最后才退出程序。
雖然簡單的例子,但就輸入的測試數據來說,假設每一次判斷都會消耗一個CPU時間,那么程序p4_1.py 的代碼要比其余的程序多耗費400%的CPU資源。
4.3 避免懸掛else問題
使用Python開發沒有這方面的考慮
age = 20
score
=
"
A
"
if
age < 18
:
if
score ==
"
A
"
:
print
(
"
恭喜你,獲得青少年一等獎!
"
)
else
:
print
(
"
抱歉,本活動只限于小于18周歲的青少年參與。
"
)
?
4.4 條件表達的三元素
>>> x = 5
>>> y = 4
>>> if x < y:
... small = x
... else:
... small = y
...
>>> small
4
>>>
三元操作符
a = x 條件 else y
表示當條件為true的時候,a被賦值為x,否則被賦值為y
>>> x = 5
>>> y = 4
>>> small = x if x < y else y
>>> small
4
>>>
?
score = int(input(
"
請您輸入一個數字:
"
))
level
=
0
if
100 >= score >= 90
:
level
=
'
A
'
elif
90 > score >= 80
:
level
=
'
B
'
elif
80 > score >= 60
:
level
=
'
C
'
elif
60 > score >=
0:
level
=
'
D
'
else
:
print
(
"
輸入錯誤!
"
)
print
(level)
如果使用三元操作符實現
score = int(input(
"
請您輸入一個數字:
"
))
level
=
0
level
=
'
A
'
if
100 >= score >= 90
else
'
B
'
if
90 > score >= 80
else
'
C
'
if
80 > score >= 60
else
'
D
'
if
60 > score >=0
else
print
(
"
輸入錯誤!
"
)
print
(level)
?4.5 斷言
斷言的語法語法其實有點像if條件的分支語句,一般在程序調試的時候,使用
>>> assert 3 < 4
>>> assert 3 > 4
Traceback (most recent call last):
File "
AssertionError
>>>
?
4.6 while循環語句
語法:
while 條件:
循環體
i = 1
sum
=
0
while
i <= 100
:
sum
+=
i
i
+= 1
print
(sum)
?
4.7 for 循環語句
語法:
for 變量 in 可迭代對象:
循環體
sum =
0
for
i
in
range(101
):
sum
+=
i;
i
+= 1
print
(sum)
range() 是一個BIF,它可以為指定的整數生成一個數字序列,語法如下:
range(stop)
range(start,stop)
range(start,stop,step)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5,10))
[5, 6, 7, 8, 9]
>>> list(range(0,10,2))
[0, 2, 4, 6, 8]
>>> list(range(0,-10,-2))
[0, -2, -4, -6, -8]
?
4.8 break 語句
break語句終止當前循環,跳出循環體
bingo =
"
清蒸
"
answer
= input(
"
小甲魚是清蒸好吃還是燉了好吃?
"
)
while
True:
if
answer ==
bingo:
break
answer
= input(
"
抱歉,請重新輸入。
"
)
print
(
"
對嗎,只有清蒸才原汁原味。
"
)
打印2018年之后出現的第一個閏年
(當年月份可以被4整除,且不能被100整除,或者可以被400整除,該年為閏年)
for
year
in
range(2018,2100
):
if
(year % 4 == 0)
and
(year % 100 != 0)
or
(year % 400 ==
0):
break
print
(
"
2018年之后出現的第一個潤年是
"
, year)
?
4.9 continue 語句
跳出本輪循環,進行下一輪循環
如果要打印2018年到2100年之間的年份
for
year
in
range(2018,2050
):
if
(year % 4 == 0)
and
(year % 100 != 0)
or
(year % 400 ==
0):
print
(year)
continue
2020
2024
2028
2032
2036
2040
2044
2048
?
4.10 else語句
?
while 條件:
循環體
else:
條件不成立時執行的內容
?
for 變量 in 可迭代對象:
循環體
else:
條件不成立可執行的內容
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

