日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Pythonif、elif、else條件

默認(rèn)情況下,腳本中的語(yǔ)句從第一個(gè)到最后一個(gè)按順序執(zhí)行。如果處理邏輯需要,可以通過(guò)兩種方式改變順序流程:

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的江蘇網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

Python 使用if關(guān)鍵字實(shí)現(xiàn)決策控制。Python 有條件執(zhí)行塊的語(yǔ)法如下:

Syntax:

if [boolean expression]:
    statement1
    statement2
    ...
    statementN

任何評(píng)估為TrueFalse的布爾表達(dá)式都會(huì)出現(xiàn)在if關(guān)鍵字之后。使用:符號(hào),并在表達(dá)式后按回車鍵,以增加的縮進(jìn)開(kāi)始一個(gè)塊。一個(gè)或多個(gè)以相同縮進(jìn)級(jí)別編寫的語(yǔ)句將被執(zhí)行if布爾表達(dá)式的計(jì)算結(jié)果為True。

要結(jié)束塊,請(qǐng)減少縮進(jìn)。塊后的后續(xù)語(yǔ)句將在if條件之外執(zhí)行。 以下示例演示了if條件。

Example: if Condition

price = 50

if price < 100:
    print("price is less than 100") 

Output

price is less than 100

在上例中,表達(dá)式price < 100的計(jì)算結(jié)果為True,因此它將執(zhí)行該塊。 if塊從:之后的新行開(kāi)始,并且if條件下的所有語(yǔ)句都以增加的縮進(jìn)開(kāi)始,無(wú)論是空格還是制表符。 以上,if塊只包含一條語(yǔ)句。下面的示例在 if 條件中有多個(gè)語(yǔ)句。

Example: Multiple Statements in the if Block

price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity) 

Output

price*quantity is less than 500
price = 50
quantity = 5

上圖中,if 條件包含多個(gè)縮進(jìn)相同的語(yǔ)句。如果所有語(yǔ)句都不在同一個(gè)縮進(jìn)中,無(wú)論是空格還是制表符,那么它都會(huì)引發(fā)IdentationError。

Example: Invalid Indentation in the Block

price = 50
quantity = 5
if price*quantity < 500:
    print("price is less than 500")
    print("price = ", price)
     print("quantity = ", quantity) 

Output

 print("quantity = ", quantity)
 ^
IdentationError: unexpected indent 

if條件具有相同縮進(jìn)級(jí)別的語(yǔ)句將不在 if 塊中考慮。他們會(huì)考慮退出if狀態(tài)。

Example: Out of Block Statements

price = 50
quantity = 5
if price*quantity < 100:
    print("price is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
print("No if block executed.") 

Output

No if block executed. 

下面的示例演示了多個(gè) if 條件。

Example: Multiple if Conditions

price = 100

if price > 100:
 print("price is greater than 100")

if price == 100:
  print("price is 100")

if price < 100:
    print("price is less than 100") 

Output

price is 100

請(qǐng)注意,每個(gè)if塊包含不同縮進(jìn)的語(yǔ)句,這是有效的,因?yàn)樗鼈儽舜瞬煌?/p>

*Note*It is recommended to use 4 spaces or a tab as the default indentation level for more readability. *## 其他條件

如果if條件中的布爾表達(dá)式計(jì)算結(jié)果為False,則else條件可以與if語(yǔ)句一起用于定義要執(zhí)行的替代語(yǔ)句塊。

Syntax:

if [boolean expression]:
    statement1
    statement2
    ...
    statementN
else:
    statement1
    statement2
    ...
    statementN

如前所述,縮進(jìn)塊從:符號(hào)之后開(kāi)始,在布爾表達(dá)式之后。當(dāng)條件為True時(shí)執(zhí)行。 當(dāng)if條件為False時(shí),我們還有另一個(gè)塊需要執(zhí)行。 首先用退格完成if塊并寫else,在新塊前面加上:符號(hào)開(kāi)始,并在塊中加上所需語(yǔ)句。

Example: else Condition

price = 50

if price >= 100:
    print("price is greater than 100")
else:
    print("price is less than 100") 

Output

price is less than 100

在上面的例子中,如果條件price >= 100False,那么將執(zhí)行else塊。else 塊還可以包含多個(gè)縮進(jìn)相同的語(yǔ)句;否則會(huì)升高IndentationError。

注意不能有多個(gè)else塊,必須是最后一個(gè)塊。

elif 條件

使用elif條件用于在if條件之后或在ifelse條件之間包含多個(gè)條件表達(dá)式。

Syntax:

if [boolean expression]:
    [statements]
elif [boolean expresion]:
    [statements]
elif [boolean expresion]:
    [statements]
else:
    [statements]            

如果指定條件評(píng)估為True,則執(zhí)行elif塊。

Example: if-elif Conditions

price = 100

if price > 100:
    print("price is greater than 100")
elif price == 100:
    print("price is 100")
elif price < 100:
    print("price is less than 100") 

Output

price is 100

在上例中,elif條件在if條件之后應(yīng)用。 Python 將評(píng)估if條件,如果評(píng)估為False,則評(píng)估elif塊并執(zhí)行表達(dá)式評(píng)估為Trueelif塊。 如果多個(gè)elif條件變?yōu)?code>True,則執(zhí)行第一個(gè)elif塊。

以下示例演示 ifelif、else條件。

Example: if-elif-else Conditions

price = 50

if price > 100:
    print("price is greater than 100")
elif price == 100:
    print("price is 100")
else price < 100:
    print("price is less than 100") 

Output

price is less than 100

所有的 if、elifelse條件必須從相同的縮進(jìn)級(jí)別開(kāi)始,否則會(huì)提升IndentationError

Example: Invalid Indentation

price = 50

if price > 100:
    print("price is greater than 100")
 elif price == 100:
    print("price is 100")
  else price < 100:
    print("price is less than 100") 

Output

 elif price == 100:
                    ^
IdentationError: unindent does not match any outer indentation level 

嵌套的 if、elif、else 條件

Python 支持嵌套的 ifelif、else條件。內(nèi)部條件必須比外部條件具有更大的縮進(jìn),并且一個(gè)塊下的所有語(yǔ)句都應(yīng)該具有相同的縮進(jìn)。

Example: Nested if-elif-else Conditions

price = 50
quantity = 5
amount = price*quantity

if amount > 100:
    if amount > 500:
        print("Amount is greater than 500")
    else:
        if amount < 500 and amount > 400:
            print("Amount is")
        elif amount < 500 and amount > 300:
            print("Amount is between 300 and 500")
        else:
            print("Amount is between 200 and 500")
elif amount == 100:
    print("Amount is 100")
else:
    print("Amount is less than 100") 

Output

Amount is between 200 and 500

當(dāng)前名稱:Pythonif、elif、else條件
標(biāo)題鏈接:http://www.5511xx.com/article/dpiophp.html