အရင်အခန်းတွေမှာ ကျွန်တော်တို့ ရေးခဲ့တဲ့ program တွေက အပေါ်ကနေ အောက်ကို တစ်ကြောင်းချင်း တိုက်ရိုက် run သွားတာချည်းပဲ။ ဒါပေမယ့် တကယ့်ဘဝမှာ ကွန်ပျူတာက "ဆုံးဖြတ်ချက်" တွေ ချရတယ်။ ဥပမာ — အမှတ် ၄၀ ရရင် အောင်တယ်၊ မရရင် ကျတယ်။ အသက် ၁၈ ပြည့်ပြီးရင် မဲပေးလို့ရတယ်၊ မပြည့်သေးရင် မရဘူး။ ဒီလို "အခြေအနေပေါ် မူတည်ပြီး မတူညီတဲ့ အလုပ် လုပ်တာ" ကို programming မှာ condition (အခြေအနေ) လို့ ခေါ်ပြီး၊ Python မှာ if, else, elif တို့နဲ့ ရေးပါတယ်။
ဒီအခန်းပြီးရင် condition တွေကို ယုံကြည်မှုရှိရှိ ရေးနိုင်သွားမှာပါ။ ဥပမာတိုင်းကို ကိုယ်တိုင် ကွန်ပျူတာမှာ ရိုက်ထည့်ပြီး run ကြည့်ပါ — ဖတ်ရုံနဲ့ မရဘဲ လက်တွေ့ စမ်းကြည့်မှ တကယ် တတ်မြောက်မှာပါ။
ဒီအခန်းပြီးရင်...
ifstatement ရဲ့ syntax နဲ့ indentation ရဲ့ အရေးပါမှုကို နားလည်မယ်elseနဲ့ နှစ်ဖက် ဆုံးဖြတ်ချက်တွေ ရေးနိုင်မယ်elifနဲ့ အခြေအနေများစွာကို အစဉ်လိုက် စစ်နိုင်မယ် (first-true-wins သဘောတရား အပါအဝင်)- nested if နဲ့ ရှုပ်ထွေးတဲ့ ဆုံးဖြတ်ချက်တွေ တည်ဆောက်နိုင်မယ်
- shorthand if နဲ့ ternary expression ကို နေရာမှန်မှာ သုံးနိုင်မယ်
getpassနဲ့ လျှို့ဝှက်စကားဝှက် ရိုက်ထည့်ခိုင်းတဲ့ login program ရေးနိုင်မယ်
if ဆိုတာ ဘာလဲ
if statement က ကွန်ပျူတာကို အခြေအနေ တစ်ခု ပေးလိုက်တာပါ။ အခြေအနေ မှန်ရင် (True ဖြစ်ရင်) if အောက်က code block ကို run တယ်။ မှားရင် (False ဖြစ်ရင်) အဲဒီ code block ကို ကျော်သွားတယ်။ အခြေအနေဆိုတာ အမြဲတမ်း Boolean expression — ဆိုလိုတာက အဖြေက True ဒါမှမဟုတ် False ပဲ ထွက်တဲ့ စကားစုပါ။
Syntax က ဒီလိုပါ — if keyword နောက် condition၊ ပြီးရင် colon (:)၊ ပြီးရင် နောက် code တွေကို အထဲကို တွန်းထည့်ရတယ် (indentation)။
score = 78
pass_mark = 40
if score >= pass_mark:
print("You passed the exam!")
You passed the exam!
score >= pass_mark ဆိုတာ 78 >= 40 ဖြစ်လို့ True ထွက်တယ်။ ဒါကြောင့် if အောက်က စာကြောင်း run သွားတယ်။ score က ၃၀ ဖြစ်ခဲ့ရင် condition က False ဖြစ်ပြီး ဘာမှ print မဖြစ်ဘဲ program က တိတ်တိတ်လေး ဆက်သွားမှာပါ။
မှတ်ချက်
if နောက်က condition နေရာမှာ comparison operator (>, <, >=, <=, ==, !=) တွေနဲ့ logical operator (and, or, not) တွေ သုံးတာ အများဆုံးပါ။ အဲဒါတွေကို Chapter 3 မှာ လေ့လာပြီးသားပါ။
သတိပြုရန်
Python မှာ indentation (စာကြောင်းအစက နေရာလွတ်) က အသက်ပါ။ if အောက်က code တွေကို space ၄ ချက် (ဒါမှမဟုတ် tab တစ်ခု) တွန်းထည့်ရမယ်။ တွန်းထည့်တာ မတူရင် program က error တက်ပါလိမ့်မယ်။
Reference
- Operators — condition တွေမှာ သုံးတဲ့ comparison နဲ့ logical operator တွေ ပြန်ကြည့်ဖို့
- Built-in Types —
True/Falseဆိုတဲ့ Boolean တန်ဖိုးတွေရဲ့ တရားဝင် ရှင်းလင်းချက်
Condition ရေးနည်း — Comparison နဲ့ Logical Operator တွေ
if ရဲ့ အသက်က condition ပါ။ Condition ကောင်းကောင်း ရေးတတ်မှ program က မှန်မှန် ဆုံးဖြတ်နိုင်မယ်။ Chapter 3 မှာ သင်ခဲ့တဲ့ operator တွေကို ဒီမှာ ပြန်အသုံးချရမယ်။
Comparison operator ၆ ခု — နှိုင်းယှဉ်ပြီး True/False ထုတ်ပေးတယ်။
| Operator | အဓိပ္ပာယ် | ဥပမာ |
|---|---|---|
> |
ထက် ကြီးတယ် | 5 > 3 → True |
< |
ထက် ငယ်တယ် | 5 < 3 → False |
>= |
ကြီးတယ် သို့မဟုတ် ညီတယ် | 5 >= 5 → True |
<= |
ငယ်တယ် သို့မဟုတ် ညီတယ် | 4 <= 5 → True |
== |
ညီမျှတယ် | 5 == 5 → True |
!= |
မညီဘူး | 5 != 3 → True |
Logical operator ၃ ခု — condition အများကြီးကို ဆက်စပ်ပေးတယ်။
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("You may enter the concert.")
else:
print("Sorry, you cannot enter.")
You may enter the concert.
and က နှစ်ခုလုံး True မှ True၊ or က တစ်ခုခု True ရင် True၊ not က ပြောင်းပြန်လှန်ပေးတယ်။ Condition နေရာမှာ Boolean variable ကို တိုက်ရိုက်လည်း ထည့်လို့ရတယ် —
is_raining = False
if is_raining:
print("Take an umbrella.")
else:
print("Enjoy the sunshine.")
Enjoy the sunshine.
သတိပြုရန်
= နဲ့ == ကို ရောထွေးတာ အဖြစ်အများဆုံး အမှားပါ။ = က တန်ဖိုးထည့်တာ (assignment)၊ == က နှိုင်းယှဉ်တာ (comparison)။ if age = 20: လို့ ရေးမိရင် Python က ချက်ချင်း SyntaxError ပြပါလိမ့်မယ်။
Reference
- Operators — comparison နဲ့ logical operator တွေရဲ့ အသေးစိတ်
- Built-in Types — Boolean (
True/False) တန်ဖိုးတွေရဲ့ တရားဝင် ရှင်းလင်းချက်
else နဲ့ နှစ်ဖက် ဆုံးဖြတ်ခြင်း
if တစ်ခုတည်းဆိုရင် condition မှန်တဲ့အခါပဲ တစ်ခုခု လုပ်လို့ရတယ်။ မှားတဲ့အခါမှာလည်း တခြားတစ်ခုခု လုပ်ချင်ရင် else ကို သုံးပါတယ်။ else က သူ့ဘာသာ condition မပါဘူး — if ရဲ့ condition False ဖြစ်ရင် အလိုအလျောက် run တာပါ။
water_depth = 1.2
safe_depth = 1.5
if water_depth <= safe_depth:
print("Safe to swim here.")
else:
print("Too deep! Please stay in the shallow area.")
Safe to swim here.
water_depth ကို ၂.၀ လို့ ပြောင်းကြည့်ပါ။ condition က False ဖြစ်သွားပြီး else အောက်က စာကြောင်းပဲ run ပါလိမ့်မယ်။ if နဲ့ else မှာ တစ်ခုပဲ အမြဲ run တယ် — နှစ်ခုလုံး တစ်ပြိုင်နက် run တာမျိုး မရှိပါဘူး။
User ဆီက တန်ဖိုးယူပြီး စစ်တာက ပို လက်တွေ့ကျတယ်။
score = int(input("Enter your score: "))
if score >= 40:
print(f"Pass! Your score is {score}.")
else:
print(f"Fail. Your score is {score}. Keep trying!")
Enter your score: 65
Pass! Your score is 65.
input() က string ပြန်ပေးလို့ int() နဲ့ ဂဏန်းပြောင်းရတာကို သတိထားပါ — Chapter 2 မှာ သင်ခဲ့ပြီးသားပါ။
မှတ်ချက်
else: နောက်မှာလည်း colon (:) ပါတယ်၊ အောက်က code ကိုလည်း indent လုပ်ရမယ်ဆိုတာ မမေ့ပါနဲ့။ else ကို if နဲ့ တွဲသုံးရတယ် — တစ်ခုတည်း သီးသန့် ရှိနေလို့ မရပါဘူး။
Reference
- Operators —
<=,==လို comparison တွေ condition မှာ ဘယ်လို အလုပ်လုပ်လဲ ပြန်ကြည့်ဖို့ - User Input — user ဆီက တန်ဖိုးယူပြီး
if/elseနဲ့ စစ်တဲ့ program တွေ ရေးဖို့
elif — အခြေအနေများစွာကို အစဉ်လိုက် စစ်ခြင်း
တကယ့်ဘဝမှာ ဆုံးဖြတ်ချက်တွေက နှစ်ဖက်တည်း မဟုတ်ပါဘူး။ ရုပ်ရှင်လက်မှတ် ဈေးနှုန်းကို ကြည့်ပါ — အသက်အလိုက် ဈေးကွာတယ်။ ဒီလို အခြေအနေ အများကြီးကို elif (else + if ရဲ့ အတိုကောက်) နဲ့ တစ်ခုပြီးတစ်ခု စစ်ပါတယ်။ elif ကို လိုသလောက် အများကြီး ထပ်ထည့်လို့ရတယ် — အခြေအနေ ဆယ်ခု ရှိရင် elif ကိုးခု ရေးလို့ရတယ်။
age = 10
if age < 5:
print("Ticket is free.")
elif age < 13:
print("Child ticket: 2000 kyats.")
elif age < 60:
print("Adult ticket: 5000 kyats.")
else:
print("Senior ticket: 3000 kyats.")
Child ticket: 2000 kyats.
အလုပ်လုပ်ပုံက ဒီလိုပါ — Python က အပေါ်ကနေ အောက်ကို တစ်ခုချင်း စစ်တယ်။ age < 5 က False၊ နောက် age < 13 က True ဖြစ်သွားတာနဲ့ အဲဒီ block ကို run ပြီး ကျန်တဲ့ elif/else တွေကို လုံးဝ ဆက်မစစ်တော့ပါဘူး။
အရေးကြီးတဲ့ စည်းမျဉ်းတစ်ခု ရှိတယ် — first-true-wins။ အမှန်ဖြစ်တဲ့ ပထမဆုံး condition ရဲ့ block ပဲ run တယ်၊ ကျန်တာတွေ ကျော်သွားတယ်။ ဥပမာ age = 3 ဆိုရင် age < 5 မှာ True ဖြစ်သွားပြီ — age < 13 လည်း မှန်နေပေမယ့် ဆက်မစစ်တော့ဘူး။ ဒါကြောင့် elif တွေကို အစဉ်လိုက် မှန်မှန်ကန်ကန် စီဖို့ အရေးကြီးပါတယ်။ အသေးကနေ အကြီးကို စီတာမျိုး (အပေါ်က ဥပမာလို) က အမှားကင်းတဲ့ နည်းပါ။
သတိပြုရန်
elif အစဉ်ကို မှားစီရင် logic bug ဖြစ်တတ်တယ်။ ဥပမာ if age < 60 ကို အရင်ထားမိရင် အသက် ၁၀ နှစ်ကလေးလည်း "Adult ticket" ဖြစ်သွားမှာပါ — ဘာလို့လဲဆိုတော့ 10 < 60 က True ဖြစ်ပြီး first-true-wins နဲ့ ရပ်သွားလို့ပါ။
Reference
- Operators —
elifchain တွေမှာ သုံးတဲ့ comparison operator တွေ - Built-in Types — Boolean logic ရဲ့ တရားဝင် ရှင်းလင်းချက်
Shorthand if နဲ့ Ternary Expression
if အောက်မှာ စာကြောင်း တစ်ကြောင်းတည်း ရှိရင် အဲဒါကို if နဲ့ တစ်ကြောင်းတည်း ရေးလို့ရတယ်။ ဒါကို shorthand if လို့ ခေါ်ပါတယ်။
temperature = 35
if temperature > 30: print("It is a hot day.")
It is a hot day.
if ရော else ရော တစ်ကြောင်းစီတည်း ရှိရင်တော့ ternary expression (conditional expression လို့လည်း ခေါ်တယ်) က ပိုပြီး သပ်ရပ်တယ်။ ပုံစံက value_if_true if condition else value_if_false ပါ။
age = 17
status = "Adult" if age >= 18 else "Minor"
print(status)
Minor
Ternary ကို print() ထဲမှာလည်း တိုက်ရိုက် သုံးလို့ရတယ်။
a = 2
b = 330
print("A") if a > b else print("B")
B
Ternary က variable ထဲ တန်ဖိုး ထည့်တဲ့အခါမှာလည်း အရမ်း အသုံးဝင်တယ် —
total = 120000
discount = 0.15 if total >= 100000 else 0.05
print(f"Discount rate: {discount}")
Discount rate: 0.15
မှတ်ချက် Shorthand နဲ့ ternary က code တိုပေမယ့် logic ရှုပ်ရင် ဖတ်ရ ခက်သွားတယ်။ အခြေအနေ ရိုးရှင်းပြီး စာကြောင်း တစ်ကြောင်းတည်းနဲ့ ပြီးတဲ့အခါမှပဲ သုံးပါ။
Reference
- Operators — ternary ထဲက condition တွေ ရေးဖို့ operator တွေ
- Built-in Functions —
print()လို function တွေ shorthand နဲ့ တွဲသုံးတာ
Nested if — if ထဲမှာ if
Nested if ဆိုတာ if statement တစ်ခု အထဲမှာ နောက် if statement တစ်ခု ထပ်ထည့်တာပါ။ ဆိုလိုတာက — ပထမ အခြေအနေ မှန်မှ ဒုတိယ အခြေအနေကို ဆက်စစ်မယ်။ တကယ့်ဘဝရဲ့ ဆုံးဖြတ်ချက်တော်တော်များများက ဒီပုံစံပါ။ ဥပမာ ဆိုင်မှာ discount ပေးတာကို ကြည့်ပါ — အရင် member ဟုတ်မဟုတ် စစ်တယ်၊ member မှန်မှ ဝယ်တဲ့ပမာဏအလိုက် discount ခွဲပေးတယ်။
is_member = True
total = 85000
if is_member:
if total >= 50000:
print("You get a 15% discount.")
else:
print("You get a 5% discount.")
else:
print("No discount. Please register as a member.")
You get a 15% discount.
အထဲက if ကို အပြင်က if ထက် ပိုပြီး indent လုပ်ရတယ် — အဆင့် ၂ ဆင့် တွန်းထည့်ထားတာကို သတိထားပါ။ Indentation ပဲ Python ကို "ဘယ် if က ဘယ်အထဲမှာ ရှိလဲ" ဆိုတာ ပြောပြတာပါ။
အောက်မှာ လက်တွေ့ scenario တွေနဲ့ ရေးထားတဲ့ ဥပမာ ၆ ခု ရှိပါတယ်။ တစ်ခုချင်းစီကို ကိုယ်တိုင် run ကြည့်ပြီး output ကို ခန့်မှန်းကြည့်ပါ။
ဥပမာ ၁ — ဘဏ်လက်ကျန်ငွေ စစ်ခြင်း
balance = -500
if balance > 0:
print("Your account has money.")
else:
if balance == 0:
print("Your account is empty.")
else:
print("Your account is overdrawn!")
Your account is overdrawn!
ဥပမာ ၂ — စာမေးပွဲ အမှတ်ပေးခြင်း
အရင် အောင်/ကျ စစ်တယ်။ အောင်မှ grade ခွဲတယ်။
score = 85
if score >= 40:
print("Result: Pass")
if score >= 80:
print("Grade: A")
elif score >= 60:
print("Grade: B")
else:
print("Grade: C")
else:
print("Result: Fail. Try harder next time!")
Result: Pass
Grade: A
အထဲက if/elif/else အစုဟာ အပြင်က if score >= 40 ရဲ့ block အထဲမှာ လုံးဝ ရှိနေတာကို သတိထားပါ။
ဥပမာ ၃ — Bus ကားခ တွက်ခြင်း
age = 16
has_student_card = True
if age < 6:
print("Free ride.")
else:
if has_student_card:
print("Student fare: 200 kyats.")
else:
print("Normal fare: 400 kyats.")
Student fare: 200 kyats.
ဥပမာ ၄ — ရုပ်ရှင်ပွဲ ရွေးခြင်း
day = "Saturday"
time = 20
if day == "Saturday" or day == "Sunday":
if time < 18:
print("Matinee show: 4000 kyats.")
else:
print("Evening show: 6000 kyats.")
else:
print("Weekday show: 3500 kyats.")
Evening show: 6000 kyats.
ဥပမာ ၅ — ဆိုင် login စစ်ခြင်း
username = input("Enter username: ")
password = input("Enter password: ")
if username == "kaung":
if password == "kaung123":
print("Login successful. Welcome, Kaung!")
else:
print("Wrong password. Try again.")
else:
print("Unknown user.")
username မှန်မှ password ကို ဆက်စစ်တယ် — ဒါက nested if ရဲ့ သဘာဝအတိုင်းပဲ။
ဥပမာ ၆ — အကောင့်ဖွင့်ခြင်း (validation)
name = input("Enter your name: ")
email = input("Enter your email: ")
password = input("Enter your password: ")
if name == "":
print("Name cannot be empty.")
else:
if "@" not in email:
print("Invalid email address.")
else:
if len(password) < 8:
print("Password must be at least 8 characters.")
else:
print("Registration successful. Welcome!")
အဆင့် ၃ ဆင့် nested ဖြစ်နေတာကို သတိထားပါ — name စစ်၊ ပြီးမှ email စစ်၊ ပြီးမှ password စစ်။ အမှားတစ်ခု တွေ့တာနဲ့ ကျန်တာတွေ ဆက်မစစ်တော့ဘူး။
သတိပြုရန်
Nested if တွေ အရမ်းများလာရင် code ဖတ်ရ ခက်လာတယ် ("arrow" ပုံစံ ညာဘက်ကို ရွေ့ရွေ့သွားတာ)။ အဆင့် ၃ ဆင့်ထက် ပိုရင် logic ကို ပြန်စဉ်းစားသင့်ပြီ — elif နဲ့ ရေးလို့ရမလား၊ ဒါမှမဟုတ် နောင် Chapter တွေမှာ သင်ရမယ့် function ထဲ ခွဲထုတ်လို့ရမလား စဉ်းစားပါ။
Reference
- Operators — nested condition တွေမှာ သုံးတဲ့
and,or,not inတို့ - Built-in Functions — validation မှာ သုံးတဲ့
len()function
getpass — စကားဝှက် လျှို့ဝှက်ရိုက်ထည့်ခိုင်းခြင်း
Login program တွေမှာ စကားဝှက် ရိုက်တဲ့အခါ screen ပေါ်မှာ စာလုံးတွေ ပေါ်မလာစေချင်ဘူး။ Python ရဲ့ getpass module က ဒီအတွက် — getpass.getpass() နဲ့ တောင်းရင် ရိုက်တဲ့စာတွေ မပေါ်ဘဲ လျှို့ဝှက်ထားပေးတယ်။
import getpass
saved_user = "cashier"
saved_code = "shop2026"
username = input("Enter username: ")
password = getpass.getpass("Enter password: ")
if username == saved_user and password == saved_code:
print("Access granted. Welcome!")
else:
print("Access denied. Incorrect username or password.")
Enter username: cashier
Enter password:
Access granted. Welcome!
Enter password: မှာ စကားဝှက် ရိုက်နေပေမယ့် screen ပေါ်မှာ ဘာမှ မပေါ်ပါဘူး — ဒါက getpass ရဲ့ အလုပ်ပါ။ username == saved_user and password == saved_code ဆိုတဲ့ condition မှာ and သုံးထားတာကို သတိထားပါ — နှစ်ခုလုံး မှန်မှ access ရတာပါ။
မှတ်ချက်
getpass က terminal/command prompt မှာ run တဲ့အခါ အကောင်းဆုံး အလုပ်လုပ်တယ်။ IDLE လို editor တချို့မှာတော့ warning ပြပြီး ပုံမှန် input() လို ပေါ်သွားတတ်တယ်။
Login စစ်တဲ့အခါ and အစား or သုံးမိရင် အန္တရာယ်ရှိတယ် — username == saved_user or password == saved_code လို့ ရေးမိရင် username မှန်ရုံနဲ့ ဝင်လို့ရသွားမယ်။ နှစ်ခုလုံး မှန်မှ ဝင်ခွင့်ပေးချင်ရင် and ပဲ သုံးရမယ်။ ဒီလို သေးသေးလေး မှားတာက လုံခြုံရေးမှာ အကြီးအကျယ် ကွာသွားတတ်တယ်။
Reference
- User Input —
input()ရဲ့ အခြေခံ အလုပ်လုပ်ပုံ - Built-in Types — string နှိုင်းယှဉ်ခြင်း (
==) ရဲ့ အဓိပ္ပာယ်
elif နဲ့ Nested if — ဘယ်အချိန် ဘယ်ဟာ သုံးမလဲ
Beginner တွေ အမေးများတဲ့ မေးခွန်း — အခြေအနေများစွာ ရှိရင် elif သုံးရမလား၊ nested if သုံးရမလား။ စည်းမျဉ်း ရှင်းရှင်းလေး ရှိတယ်။
elif— အခြေအနေတွေက တစ်ခုနဲ့တစ်ခု သီးသန့် ဖြစ်တဲ့အခါ။ ဥပမာ အသက်အလိုက် လက်မှတ်ဈေး — လူတစ်ယောက်က အသက်အရွယ်အစား တစ်ခုတည်းမှာပဲ ရှိနိုင်တယ်။- Nested
if— ဒုတိယ အခြေအနေက ပထမ အခြေအနေ မှန်ပြီးမှ အဓိပ္ပာယ် ရှိတဲ့အခါ။ ဥပမာ member မှန်မှ discount ခွဲတာ၊ username မှန်မှ password ဆက်စစ်တာ။
Exam grading ဥပမာကို ပြန်ကြည့်ပါ — အပြင်က if score >= 40 က အောင်/ကျ ခွဲတယ်။ အောင်မှ grade ခွဲတဲ့ elif အတွဲလိုက် အဓိပ္ပာယ် ရှိတယ်။ ကျတဲ့သူကို grade ခွဲစရာ မလိုဘူး။ ဒါကြောင့် အပြင်မှာ if/else၊ အထဲမှာ elif — နှစ်မျိုးလုံး ရောသုံးထားတာ။
မှတ်ချက်
Nested if ကို and နဲ့ တစ်ကြောင်းတည်း ရေးလို့ရတဲ့ အခြေအနေလည်း ရှိတယ်။ ဥပမာ if is_member: အထဲက if total >= 50000: ကို if is_member and total >= 50000: လို့ ပေါင်းရေးလို့ရတယ်။ ဒါပေမယ့် else အကိုင်းတွေမှာ မက်ဆေ့ချ် မတူချင်ရင် (member မဟုတ်တဲ့သူကို တခြားစာပြချင်ရင်) nested ပုံစံကပဲ ရှင်းတယ်။
Reference
- Operators —
andနဲ့ condition တွေ ပေါင်းစပ်နည်း - Built-in Types — Boolean logic ရဲ့ တရားဝင် ရှင်းလင်းချက်
အဖြစ်များတဲ့ အမှား ၄ ခု
Condition ရေးတဲ့အခါ beginner တိုင်း တစ်ခါတော့ ကြုံရတဲ့ အမှားတွေ — ကြိုသိထားရင် အချိန်ကုန် သက်သာတယ်။
၁။ Colon (:) မေ့တာ — အောက်က မှားနေတဲ့ code ကို ကြည့်ပါ (if နောက် colon မပါဘူး)။
if score > 40
print("Pass")
if, else, elif တိုင်းရဲ့ အဆုံးမှာ colon ပါရမယ်။ မပါရင် SyntaxError တက်တယ်။
၂။ Indentation မမှန်တာ — အောက်က မှားနေတဲ့ code မှာ print ကို indent မလုပ်ထားဘူး။
if score > 40:
print("Pass")
Block အထဲက စာကြောင်းကို indent မလုပ်ရင် Python က "ဒါ if ရဲ့ အထဲက စာကြောင်း မဟုတ်ဘူးလား" လို့ မသိတော့ဘူး — IndentationError တက်တယ်။
၃။ = နဲ့ == မှားတာ — အပေါ်မှာ ပြောခဲ့ပြီးပြီ။ Condition ထဲမှာ အမြဲ == သုံးပါ။
၄။ elif အစဉ် မှားတာ — ဒါက error မတက်ဘဲ logic မှားတဲ့ အန္တရာယ်အရှိဆုံး အမှားပါ။
age = 10
if age < 60:
print("Adult ticket: 5000 kyats.")
elif age < 13:
print("Child ticket: 2000 kyats.")
အသက် ၁၀ နှစ်ကလေးကို "Adult ticket" လို့ ပြသွားမယ် — ဘာလို့လဲဆိုတော့ 10 < 60 က True ဖြစ်ပြီး first-true-wins နဲ့ ရပ်သွားလို့ပါ။ အသေးကနေ အကြီးကို အစဉ်လိုက် စီပါ။
မှတ်ချက်
Program run ပြီး ရလဒ် မှားနေရင် အရင် condition တွေကို print() နဲ့ ထုတ်ကြည့်ပါ။ ဥပမာ print(age < 60) လို့ ထည့်ကြည့်ရင် ဘယ် condition က True ဖြစ်နေလဲ ချက်ချင်း သိရတယ်။ ဒါကို debugging လို့ ခေါ်တယ်။
အခန်း အကျဉ်းချုပ်
ဒီအခန်းမှာ condition နဲ့ ဆုံးဖြတ်ချက်ချတဲ့ အတတ်ပညာ အပြည့်အစုံကို သင်ခဲ့ပြီးပြီ။ ပြန်ကောက်ကြည့်မယ် —
if— conditionTrueဖြစ်ရင် block run တယ်။ Colon နဲ့ indentation မဖြစ်မနေ လိုတယ်။else—ifရဲ့ conditionFalseဖြစ်ရင် run တယ်။ သူ့ဘာသာ condition မပါဘူး။elif— အခြေအနေများစွာကို အပေါ်ကနေ အောက်ကို အစဉ်လိုက် စစ်တယ်။ First-true-wins — အမှန်ဖြစ်တဲ့ ပထမဆုံး block ပဲ run တယ်။- Shorthand
if/ ternary — စာကြောင်း တစ်ကြောင်းတည်းနဲ့ ပြီးတဲ့ ရိုးရှင်းတဲ့ အခြေအနေတွေအတွက်။ - Nested
if—ifထဲမှာif။ ဒုတိယ အခြေအနေက ပထမ အခြေအနေ မှန်ပြီးမှ အဓိပ္ပာယ် ရှိတဲ့အခါ သုံးတယ်။ getpass— စကားဝှက် လျှို့ဝှက်ရိုက်ခိုင်းတဲ့ login program တွေ ရေးနိုင်ပြီ။
နောက် Chapter (Chapter 5) မှာ ဒီ condition တွေကို while loop တွေနဲ့ တွဲပြီး ထပ်ခါထပ်ခါ ဆုံးဖြတ်ခိုင်းတဲ့ အတတ်ပညာ သင်ရမယ် — ATM program လို တကယ့် program တွေ စရေးတော့မှာပါ။ အခု လေ့ကျင့်ခန်းတွေကို ကိုယ်တိုင် ရေးကြည့်ပြီး အဖြေမှန်မမှန် run စစ်ကြည့်ပါ။
လေ့ကျင့်ခန်းများ
၁။ မီတာခ တွက်ခြင်း — user ဆီက သုံးစွဲတဲ့ electric unit အရေအတွက် ယူပြီး မီတာခ တွက်တဲ့ program ရေးပါ။ နှုန်းထားက —
- unit ၁၀၀ အောက် (သို့မဟုတ် ညီမျှ) — အခမဲ့
- နောက် unit ၂၀၀ (၁၀၁–၃၀၀) — တစ်ယူနစ် ၅၀ ကျပ်
- unit ၃၀၀ အထက် — တစ်ယူနစ် ၇၀ ကျပ်
ဥပမာ unit ၇၀၀ သုံးရင် စုစုပေါင်း = 0 + (200 × 50) + (400 × 70) = 38,000 ကျပ်။
၂။ အလယ်တန်ဖိုး ရှာခြင်း — integer ၃ ခု ထည့်ခိုင်းပြီး numX, numY, numZ ထဲ သိမ်းပါ။ if/elif/else သုံးပြီး အဲဒီ ၃ ခုထဲက အလယ်တန်ဖိုး (အကြီးဆုံးလည်း မဟုတ်၊ အသေးဆုံးလည်း မဟုတ်) ကို print ထုတ်ပါ။
၃။ ဂဏန်း ၃ လုံး စစ်ခြင်း — ဂဏန်း ၃ လုံး ထည့်ခိုင်းပါ။ ဘယ်ဘက်ဆုံး digit က မ (odd) ဆိုရင် "The number starts with an odd digit." လို့၊ စုံ (even) ဆိုရင် "The number starts with an even digit." လို့ print ပါ။ ဂဏန်း ၃ လုံး မဟုတ်ရင် "Wrong input! Try again." လို့ ပြပါ။
၄။ အမှတ်မှ grade သို့ — စာမေးပွဲ အမှတ် (0–100) ထည့်ခိုင်းပြီး grade ထုတ်တဲ့ program ရေးပါ။ 80+ → A, 60+ → B, 40+ → C, ကျန် → F။ အမှတ် 0–100 ပြင်ပ ဖြစ်နေရင် "Invalid score." လို့ ပြပါ။
၅။ ဆိုင် discount တွက်စက် — ဝယ်ယူစုစုပေါင်း ပမာဏ ထည့်ခိုင်းပါ။ member ဟုတ်/မဟုတ် မေးပါ (yes/no)။ Member ဖြစ်ပြီး 100,000+ ဝယ်ရင် 15% discount၊ member ဖြစ်ပြီး 100,000 အောက် ဝယ်ရင် 5% discount၊ member မဟုတ်ရင် discount မရှိ။ နောက်ဆုံး ပေးရမယ့် ဈေးနှုန်းကို f-string နဲ့ လှလှပပ print ထုတ်ပါ။