Rea u amohela thutong ea botšelela letotong lena. Hona joale re kena ka hare ho lenaneo mme re tla ithuta ho sebelisa "haeba lipolelo" ho Python.
"haeba lipolelo" li bohlokoa haholo lenaneong kaha li re lumella ho theha mananeo a ka etsang liqeto ho latela maemo a itseng.
Haeba Polelo
"Haeba polelo" e sebelisetsoa ho etsa lenaneo le thehiloeng liqetong tse 'maloa. Ka mohlala, bala sena:
“Haeba ho chesa,
Ke letsatsi le chesang
Noa metsi a mangata
Ho seng joalo, haeba ho bata
Ke letsatsi le batang
apara liaparo tse futhumetseng
Ho seng joalo, ke letsatsi le monate”
Lipolelong tse ka holimo, re etsa liqeto tse ling ho latela maemo a itseng. Polelo ea pele ke boemo, ea bobeli ke moelelo oa eona 'me ea boraro e phethahatsa qeto.
Lipolelo tsa haeba li sebetsa ka mokhoa o ts'oanang.
Ha re e shebeng lenaneong.
Re hlahisa mofuta oa boolean.
is_hot = true
Joale re tla eketsa "haeba polelo"
if is_hot:
print("It's a hot day.")
Hona joale polelo ena e tla etsoa haeba boemo bo ka holimo e le 'nete. Ho tsoa ho "haeba polelo" u tla tlameha ho tobetsa "Shift + Tab" ho etsa hore khesara ea hau e be moleng oa mohala o mocha oa khoutu.
Jwale, ha re tsamaiseng lenaneo lena.
Joale ha re bapale ka lenaneo lena.
Bona hore na ho etsahala'ng haeba ke fetola boemo bo ka holimo hore e be leshano.
Ho hlakile hore haeba boemo bo fosahetse, "haeba polelo" ha e phethoe. Joale re tla eketsa boemo ba bobeli bo tla etsoa haeba boemo ba pele bo sa phethahale.
is_hot = False
if is_hot:
print("It's a hot day.")
print("Drink plenty of water")
else:
print("It's a cold day.")
print("Wear warm clothes.")
print("Enjoy your day")
Boemo ba "haeba" bo fosahetse ka hona mofetoleli o tla ea sehlopheng se latelang sa likhoutu tlas'a "indent" e 'ngoe.
"Haeba polelo" ha se hakaalo hore e lekanyelitsoe ho maemo a mabeli feela. U ka kena maemong a mangata kamoo u ka ratang. Ha re kenye maemo a mang lenaneong lena.
Re hlalosa boemo bo bong ka phetoho ea boolean. E tlohelle:
if_cold = True
Hona joale re ka eketsa boemo ba boraro lenaneong la rona. E sebetsa joalo ka sena
is_hot = False
if_cold = True
if is_hot:
print("It's a hot day.")
print("Drink plenty of water")
elif is_cold:
print("It's a cold day.")
print("Wear warm clothes.")
else:
print("It's a lovely day")
print("Enjoy your day")
Joale haeba e se letsatsi le chesang kapa le batang, ke letsatsi le monate. Mona re sebelisitse lentsoe le lecha "elif" ho hlalosa boemo bo bocha pakeng tsa pele "haeba" le "esele" ea ho qetela. U ka eketsa "elif" tse ngata kamoo u ratang kateng.
ho ikoetlisa
tharollo
Phethela!
Ke tšepa hore lōna banna le thabela letoto lena. Puo e latelang e tla ba mabapi le tšebeliso ea basebetsi Python.
Khotso!
Leave a Reply