Conditional yog cov lus hauv Python. Syntax, else/elif blocks, piv txwv

Nyob rau hauv tus txheej txheem ntawm kev kawm mus rau qhov kev pab cuam, nws yog feem ntau tsim nyog los tsim cov kev pab cuam uas tsis yog li yooj yim rau siv nyob rau hauv lub neej tiag tiag. Tom qab tag nrho, qee lub sij hawm koj yuav tsum ua raws li cov lus qhia tsuas yog nyob rau hauv qee yam mob. Yuav kom siv tau qhov no hauv qhov kev zov me nyuam, txhua hom lus muaj cov lus tswj hwm. Nrog lawv cov kev pab, koj tuaj yeem tswj cov kev khiav dej num ntawm kev ua tiav, tsim cov voj voog lossis ua qee yam haujlwm tsuas yog thaum muaj qee yam muaj tseeb.

Niaj hnub no peb yuav tham txog cov lus hais, uas kuaj xyuas qhov xwm txheej tam sim no rau qee yam mob, thiab raws li cov ntaub ntawv no, txiav txim siab txog kev nqis tes ua ntxiv.

Hom Cov Lus Qhia

Feem ntau, yog tias tsis yog tib nqe lus uas tswj kev khiav ntawm ib qho kev pab cuam. Zoo li nws tus kheej tuaj yeem ua ib feem ntawm cov saw loj ntawm cov neeg ua haujlwm.

Kuj tseem muaj cov voj voog thiab nqe lus uas tswj cov txheej txheem ntawm nws qhov kev ua tiav. Niaj hnub no peb tsuas yog tham txog tus neeg teb xov tooj raws li txoj cai thiab cov chains uas nws tuaj yeem koom nrog.

Nyob rau hauv lub programming, muaj xws li ib yam li branching. Nws yog qhov tseeb qhov no txhais tau hais tias ib ntu ntawm cov lus txib uas raug tua tsuas yog tias muaj qee yam xwm txheej muaj tseeb. Cov txheej txheem lawv tus kheej yuav txawv:

  1. Kev sib npaug ntawm qhov sib txawv rau ib qho nqi.
  2. Ua ib qho kev txiav txim.
  3. Lub xeev daim ntawv thov (collapsed los tsis).

Lub spectrum tuaj yeem loj dua. Cov nqe lus muaj xwm txheej muaj nyob hauv ntau hom:

  1. Nrog ib ceg. Ntawd yog, ib qho kev kuaj xyuas yog ua, raws li qhov tshwm sim ntawm qee qhov kev ua tau ua.
  2. Nrog ob lossis ntau ceg. Yog tias qhov ntsuas 1 muaj tseeb, ces kos cov qauv 2. Yog tias muaj tseeb, ces kos 3. Thiab yog li ntawd, ua ntau cov tshev raws li xav tau.
  3. Nrog ob peb yam mob. Txhua yam yooj yim ntawm no. Tus neeg txhais lus kuaj xyuas ntau yam xwm txheej lossis ib qho ntawm lawv.

yog hais tias

Tus qauv ntawm cov lus yog zoo sib xws hauv txhua hom lus. Txawm li cas los xij, hauv Python, nws cov syntax txawv me ntsis ntawm lwm tus:

yog mob:

    <входящее выражение 1>

    <входящее выражение 2>

<nе входящее выражение>

Ua ntej, tus neeg teb xov tooj nws tus kheej tau tshaj tawm, tom qab ntawd cov xwm txheej hauv qab uas nws pib ua haujlwm tau sau. Qhov xwm txheej tuaj yeem yog qhov tseeb lossis tsis tseeb.

Qhov no yog ua raws li ib qho thaiv nrog cov lus txib. Yog tias tam sim ntawd ua raws li qhov kev ntsuas kom tau raws li, ces qhov sib txuas ntawm cov lus txib hu ua if block. Koj tuaj yeem siv ntau cov lus txib hauv nws.

Mloog! Lub indentation nyob rau hauv tag nrho yog block commands yuav tsum yog tib yam loj. Lub thaiv ciam teb yog txiav txim los ntawm indents. 

Raws li cov ntaub ntawv hais lus, qhov indentation yog 4 qhov chaw. 

Tus neeg ua haujlwm no ua haujlwm li cas? Thaum tus neeg txhais lus pom yog lo lus, nws tam sim ntawd xyuas cov lus tawm tsam tus neeg siv cov txheej txheem. Yog tias qhov no tshwm sim, ces nws pib nrhiav cov lus qhia thiab ua raws li lawv. Txwv tsis pub, tag nrho cov lus txib los ntawm qhov thaiv no raug hla.

Yog hais tias ib nqe lus tom qab tus mob tsis yog indented, nws yuav tsis kho raws li ib tug yog block. Hauv peb qhov xwm txheej, kab no yog . Yog li ntawd, tsis hais qhov txiaj ntsig ntawm daim tshev, kab no yuav raug tua.

Ntawm no yog ib tug code snippet rau piv txwv li cas tus neeg teb xov tooj ua hauj lwm.

number = int(input("Enter a number:"))

yog tus lej> 10:

    print ("Tus lej yog ntau dua 10")

Qhov kev pab cuam no prompts tus neeg siv rau ib tug xov tooj thiab xyuas seb nws yog ntau tshaj 10. Yog hais tias yog li ntawd, nws rov qab cov ntaub ntawv tsim nyog. Piv txwv li, yog tias tus neeg siv nkag mus rau tus lej 5, ces qhov kev pab cuam tsuas yog xaus, thiab qhov ntawd yog nws.

Tab sis yog tias koj teev tus naj npawb 100, tus neeg txhais lus yuav nkag siab tias ntau tshaj kaum, thiab qhia nws.

Mloog! Hauv peb qhov xwm txheej, yog tias qhov xwm txheej tsis raug, qhov kev zov me nyuam nres, vim tias tsis muaj cov lus txib raug muab tom qab kev qhia.

Tsuas muaj ib qho lus txib hauv cov cai saum toj no. Tab sis muaj ntau ntau ntawm lawv. Qhov tsuas yog yuav tsum tau indent.

Tam sim no cia peb txheeb xyuas qhov ntu ntawm cov lus txib.

number = int(input("Sau tus lej: "))

yog tus lej> 10:

    print("ib kab")

    print("kab thib ob")

    print("kab thib peb")

print("Txoj kab uas raug tua, tsis hais tus lej nkag")

print(“End application”)

Sim twv seb qhov tso zis yuav ua li cas yog tias koj nkag mus rau qhov tseem ceeb 2, 5, 10, 15, 50.

Raws li koj tuaj yeem pom, yog tias tus lej nkag los ntawm tus neeg siv ntau dua kaum, ces peb kab yog tso tawm + ib qho nrog cov ntawv "Khiav txhua lub sijhawm ..." thiab ib qho "End", thiab yog tias tsawg dua kaum, ces tsuas yog ib qho, nrog rau ib tsab ntawv sib txawv. Tsuas yog kab 3,4,5 yuav raug tua yog tias muaj tseeb. Txawm li cas los xij, ob kab kawg yuav raug sau txawm tias tus lej twg tus neeg siv qhia.

Yog tias koj siv cov nqe lus ncaj qha hauv lub console, qhov tshwm sim yuav txawv. Tus neeg txhais lus tam sim ntawd tig rau ntawm ntau txoj kab yog tias, tom qab qhia qhov tseeb qhov tseeb, nias Enter.

Piv txwv tias peb tau sau cov lus txib nram qab no.

>>>

>>> n = 100

> > > yog n > 10:

...

Tom qab ntawd peb yuav pom tias >>> tau hloov los ntawm ellipsis. Qhov no txhais tau hais tias multiline input hom yog enabled. Hauv cov lus yooj yim, yog tias koj nias Enter, koj yuav raug xa mus rau cov tswv yim ntawm theem thib ob ntawm kev qhia. 

Thiab txhawm rau tawm ntawm qhov thaiv no, koj yuav tsum tau ntxiv ib qho kev tsim kho ntxiv rau lub block if.

>>>

>>> n = 100

> > > yog n > 10:

… print(«nv 10»)

...

Yog tias qhov xwm txheej tsis muaj tseeb, qhov program yuav xaus. Qhov no yog ib qho teeb meem, txij li tus neeg siv yuav pom tau tias qhov kev zov me nyuam tau kaw vim qhov ua tsis tiav. Yog li ntawd, nws yog ib qho tsim nyog yuav tau muab cov lus qhia rau tus neeg siv. Rau qhov no, ib qhov txuas yog siv yog-lwm.

tus neeg ua haujlwm qhia yog-lwm

Tus neeg teb xov tooj no tso cai rau koj los siv qhov txuas: yog tias cov lus qhia sib haum rau qee txoj cai, ua cov haujlwm no, thiab yog tias tsis yog, ces lwm tus. Ntawd yog, nws tso cai rau koj faib qhov ntws ntawm qhov kev pab cuam rau hauv ob txoj kev. Lub syntax yog intuitive:

yog mob:

    #yog block

    lus 1

    lus 2

    thiab ua li ntawd

lwm yam:

    #lwm thaiv

    lus 3

    lus 4

    thiab lwm yam:

Cia peb piav qhia seb tus neeg teb xov tooj no ua haujlwm li cas. Ua ntej, tus txheej txheem nqe lus yog executed nyob rau hauv xov yees, xyuas seb nws puas sib haum mob "true" los yog "false". Kev nqis tes ua ntxiv yog nyob ntawm cov txiaj ntsig ntawm daim tshev. Yog tias muaj tseeb, cov lus qhia uas nyob rau hauv cov lus qhia ua ntu zus raws li qhov xwm txheej yog raug coj ncaj qha. yees, yog tsis muaj tseeb ces lwm tus

Txoj kev no koj tuaj yeem daws qhov yuam kev. Piv txwv li, tus neeg siv yuav tsum nkag mus rau lub vojvoog. Obviously, nws tsuas tuaj yeem yog tus lej nrog lub cim ntxiv, lossis nws yog tus nqi null. Yog tias nws tsawg dua 0, ces koj yuav tsum tau tshaj tawm cov lus nug kom koj nkag mus rau tus lej zoo. 

Nov yog txoj cai uas siv txoj haujlwm no. Tab sis muaj ib qho yuam kev ntawm no. Sim twv seb qhov twg. 

radius = int(input("Enter radius:"))

yog radius >= 0:

    print("Circumference = ", 2 * 3.14 * vojvoog)

    print("Luas = ", 3.14 * vojvoog ** 2)

    lwm yam:

        print(“Thov sau tus lej zoo”)

Indentation mismatch yuam kev. Yog thiab lwm yam yuav tsum nyob tsis muaj lawv lossis nrog tib tus lej ntawm lawv (nyob ntawm seb lawv puas nyob ze lossis tsis).

Cia peb muab lwm cov ntaub ntawv siv (qhov twg txhua yam yuav raug nrog tus neeg teb xov tooj sib dhos) - daim ntawv thov cov ntsiab lus uas kuaj xyuas tus password.

password = input("Enter password:")

yog password == «sshh»:

    print("Zoo siab txais tos")

lwm yam:

    print(“Access denied”)

Cov lus qhia no hla tus neeg ntxiv yog tias tus password yog sshh. Yog tias muaj lwm yam kev sib txuas ntawm cov tsiaj ntawv thiab cov lej, ces nws qhia cov lus "Access denied".

lus-expression if-elif-lse

Tsuas yog ob peb yam xwm txheej tsis muaj tseeb, cov nqe lus uas nyob hauv qhov thaiv raug tua. lwm tus. Qhov kev qhia no ua haujlwm zoo li no.

yog condition_1:

    #yog block

    daim ntawv qhia

    daim ntawv qhia

    ntau nqe lus

elif condition_2:

    # thawj elif thaiv

    daim ntawv qhia

    daim ntawv qhia

    ntau nqe lus

elif condition_3:

    # thib ob elif thaiv

    daim ntawv qhia

    daim ntawv qhia

    ntau nqe lus

...

lwm tus

    daim ntawv qhia

    daim ntawv qhia

    ntau nqe lus

Koj tuaj yeem hais qhia tus lej ntawm cov xwm txheej ntxiv.

Nested nqe lus

Lwm txoj hauv kev los siv ntau yam xwm txheej yog txhawm rau txheeb xyuas cov xwm txheej ntxiv rau hauv yog tias thaiv.

Tus neeg teb xov tooj if hauv lwm qhov xwm txheej thaiv

gre_score = int(input("Enter your tam sim no credit limit"))

per_grad = int(input("Enter your credit rating:"))

yog per_grad > 70:

    # sab nraud yog thaiv

        yog gre_score> 150:

            # sab hauv yog thaiv

    luam tawm ("Nyob zoo, koj tau txais nyiaj qiv")

lwm yam:

    luam tawm ("Thov txim, koj tsis tsim nyog tau txais qiv nyiaj")

Qhov kev pab cuam no ua ib daim credit ntsuam xyuas. Yog tias nws tsawg dua 70, qhov kev zov me nyuam qhia tias tus neeg siv tsis tsim nyog tau txais credit. Yog tias nws ntau dua, ib daim tshev thib ob yog ua kom pom tias qhov credit txwv tam sim no ntau dua 150. Yog tias yog, ces cov lus yuav tshwm sim tias cov nyiaj qiv tau raug xa tawm.

Yog tias ob qho txiaj ntsig tsis muaj tseeb, ces cov lus tau tshwm sim tias tus neeg siv tsis muaj peev xwm tau txais qiv nyiaj. 

Tam sim no cia peb rov ua qhov kev pab cuam ntawd me ntsis.

gre_score = int(input("Enter tam sim no txwv: "))

per_grad = int(input(“Enter credit score: “))

yog per_grad > 70:

    yog gre_score> 150:

        luam tawm ("Nyob zoo, koj tau txais nyiaj qiv")

    lwm yam:

        luam tawm ("Koj qhov credit txwv tsawg")

lwm yam:

    print ("Thov txim, koj tsis tsim nyog rau credit")

Cov cai nws tus kheej zoo sib xws, tab sis nested if kuj muab ib qho algorithm nyob rau hauv rooj plaub uas tus mob los ntawm nws hloov mus ua tsis tseeb. Ntawd yog, qhov txwv ntawm daim npav tsis txaus, tab sis keeb kwm qiv nyiaj zoo, cov lus "Koj muaj qhov qhab nia qis" tau tshwm sim.

if-lse nqe lus hauv ib qho xwm txheej lwm tus

Cia peb ua lwm txoj haujlwm uas txiav txim siab tus menyuam kawm ntawv qib raws li cov qhab nia xeem.

qhab nia = int(input("Enter your score:"))

yog qhabnias>= 90:

    print("Zoo heev! Koj qib yog A")

lwm yam:

    yog qhabnias>= 80:

print("Zoo heev! Koj qib yog B")

    lwm yam:

yog qhabnias>= 70:

    print("Zoo! Koj qib yog C")

lwm yam:

    yog qhabnias>= 60:

luam tawm ("Koj qib yog D. Nws tsim nyog rov ua cov khoom siv.")

    lwm yam:

luam tawm ("Koj ua tsis tau qhov kev xeem")

Daim ntawv thov thawj zaug kuaj xyuas seb qhov qhab nia siab dua lossis sib npaug li 90. Yog tias yog, ces nws yuav rov qab qib A. Yog tias qhov xwm txheej no tsis tseeb, tom qab ntawd kev kuaj xyuas tom qab raug ua. Peb pom tias cov algorithm yuav luag tib yam thaum xub thawj siab ib muag. Yog li es tsis txhob kuaj sab hauv lwm tus zoo dua los siv kev sib xyaw ua ke yog-elif-lse.

Yog li tus neeg ua haujlwm if ua lub luag haujlwm tseem ceeb heev - nws ua kom paub tseeb tias qee qhov ntawm cov lej raug tua tsuas yog tias muaj qhov xav tau rau nws. Nws tsis yooj yim sua kom xav txog qhov programming yam tsis muaj nws, vim tias txawm tias qhov yooj yim tshaj plaws algorithms xav tau rab rawg zoo li "yog tias koj mus rau sab laug, koj yuav pom nws, thiab yog tias koj mus rau sab xis, ces koj yuav tsum ua qhov no thiab qhov ntawd."

Sau ntawv cia Ncua