အခန်း 6 · animation 2 ခု

Chapter 5: While Loop များ (While Loops)

808 Coder မှ တင်ဆက်သည်

Chapter 4 မှာ program ကို "ဆုံးဖြတ်ချက် ချတတ်အောင်" သင်ခဲ့ပြီးပြီ။ အခု နောက် အဆင့် — "ထပ်ခါထပ်ခါ လုပ်တတ်အောင်" သင်ရမယ်။ တကယ့်ဘဝမှာ ထပ်ခါလုပ်ရတဲ့ အလုပ်တွေ အများကြီးရှိတယ် — ATM က PIN မှားရင် ထပ်ရိုက်ခိုင်းတာ၊ ဈေးတွက်ရင် ပစ္စည်း တစ်ခုချင်းစီ ပေါင်းတာ၊ သီချင်း playlist ကို တစ်ပုဒ်ပြီးတစ်ပုဒ် ဖွင့်တာ။ Programming မှာ ဒါကို loop (သို့မဟုတ် repetition / iteration) လို့ ခေါ်တယ်။

Python မှာ loop နှစ်မျိုး ရှိတယ် — while နဲ့ for။ ဒီအခန်းမှာ while loop ကို အခြေခံကနေ ကျွမ်းကျင်တဲ့အထိ လေ့လာမယ်။

ဒီအခန်းပြီးရင်...

  • while loop ရဲ့ syntax နဲ့ အလုပ်လုပ်ပုံကို နားလည်မယ်
  • definite loop နဲ့ indefinite loop ကို ခွဲခြားပြီး နေရာမှန် သုံးနိုင်မယ်
  • while...else ရဲ့ အဓိပ္ပာယ်ကို ရှင်းရှင်းလင်းလင်း သိမယ်
  • break နဲ့ loop ကို အလယ်က ရပ်နိုင်မယ်
  • PIN စစ်တဲ့ ATM program လို လက်တွေ့ program တွေ ရေးနိုင်မယ်
  • infinite loop ကို ရှောင်နိုင်မယ် (ဖြစ်လာရင်လည်း ဘယ်လို ရပ်ရမလဲ သိမယ်)

while Loop ဆိုတာ ဘာလဲ

while statement က if နဲ့ ဆင်တယ် — while keyword နောက် Boolean condition တစ်ခု ပါတယ်။ ကွာခြားချက်က — if က condition မှန်ရင် block ကို တစ်ခါ run ပြီး ပြီးသွားတာ၊ while ကတော့ condition မှန်နေသမျှ block ကို ထပ်ခါထပ်ခါ run နေတာပါ။ Block ပြီးတိုင်း condition ကို ပြန်စစ်တယ်။ မှန်နေသေးရင် နောက် တစ်ပတ်၊ မှားသွားပြီဆိုမှ loop က ထွက်ပြီး နောက် program ဆက်သွားတယ်။

count = 1

while count <= 5:
    print(f"Round {count}")
    count += 1

print("Loop finished!")
Round 1
Round 2
Round 3
Round 4
Round 5
Loop finished!

လိုက်ကြည့်ပါ — count က ၁ ကနေ စတယ်။ count <= 5 မှန်နေသမျှ print လုပ်ပြီး count ကို ၁ တိုးတယ်။ count ၆ ဖြစ်သွားတဲ့အခါ condition False ဖြစ်ပြီး loop က ထွက်သွားတယ်။ ဒီ count += 1 လို condition ကို ပြောင်းလဲပေးတဲ့ စာကြောင်းက အသက်ပါ — မပါရင် loop က ဘယ်တော့မှ မပြီးဘူး။

Loop ထဲမှာ print() ရဲ့ end parameter ကို သုံးပြီး တစ်ကြောင်းတည်း ဆက်တိုက် print လို့လည်း ရတယ်။

stars = 0

while stars < 10:
    print("*", end="")
    stars += 1
**********
A while loop keeps going while the condition is True condition? True run body loop back & check again False continue after loop the condition is checked BEFORE every round
while loop ရဲ့ flowchart

သတိပြုရန် Infinite loop (မပြီးဆုံး loop) က beginner တွေ အဖြစ်အများဆုံး အမှားပါ။ condition ကို ပြောင်းလဲပေးတဲ့ စာကြောင်း (count += 1 လို) မေ့သွားရင် program က ထာဝရ လည်နေလိမ့်မယ်။ Terminal မှာ run နေရင် Ctrl + C နှိပ်ပြီး ရပ်လို့ရတယ်။ Loop ရေးတိုင်း "ဒီ condition ဘယ်တော့ False ဖြစ်မလဲ" လို့ ကိုယ့်ကိုယ်ကို မေးပါ။

Reference

  • While Loops — while loop ရဲ့ syntax နဲ့ နမူနာတွေ
  • Operators — loop condition တွေမှာ သုံးတဲ့ comparison operator တွေ

Definite Loop vs Indefinite Loop

Loop တွေကို အမျိုးအစား နှစ်မျိုး ခွဲလို့ရတယ်။

Definite loop — loop ဘယ်နှစ်ပတ် လည်မလဲ ကြိုသိတယ်။ အပေါ်က ကြယ် ၁၀ လုံး print တဲ့ ဥပမာလိုပေါ့ — ၁၀ ပတ် လည်မယ်ဆိုတာ ရေးကတည်းက သိပြီးသား။

even = 2
times = 5

while times > 0:
    print(even)
    even += 2
    times -= 1
2
4
6
8
10

စုံကိန်း ၅ လုံး ထုတ်မယ်ဆိုတာ ကြိုသိတယ် — ဒါ definite loop ပါ။

Indefinite loop — ဘယ်နှစ်ပတ် လည်မလဲ ကြိုမသိဘူး။ user ရဲ့ လုပ်ရပ်ပေါ် မူတည်တယ်။ ဥပမာ ဈေးဝယ်စာရင်း ပေါင်းတဲ့ program — ပစ္စည်း ဘယ်နှစ်ခု ဝယ်မလဲ user ပဲ သိတယ်။ ၀ ရိုက်ထည့်မှ ရပ်မယ်လို့ သဘောတူထားတယ်။

total = 0

price = int(input("Enter item price (0 to finish): "))
while price != 0:
    total += price
    price = int(input("Enter item price (0 to finish): "))

print(f"Total: {total} kyats")
Enter item price (0 to finish): 1500
Enter item price (0 to finish): 2300
Enter item price (0 to finish): 800
Enter item price (0 to finish): 0
Total: 4600 kyats

User ၀ မရိုက်မချင်း loop က ဆက်လည်နေမယ် — ဘယ်နှစ်ပတ်လဲ ကြိုမသိဘူး။ ဒါ indefinite loop ပါ။ ATM PIN စစ်တာ၊ menu ရွေးခိုင်းတာတွေလည်း ဒီအမျိုးအစားပါပဲ။

မှတ်ချက် Loop မစခင် ပထမဆုံး input() ကို အပြင်မှာ တစ်ခါ ခေါ်ရတာကို သတိထားပါ။ ဒါမှ while ရဲ့ condition ကို ပထမအကြိမ် စစ်လို့ရမှာပါ။ ပြီးတော့ loop အထဲမှာ နောက် input() ထပ်ခေါ်ရတယ် — မဟုတ်ရင် တန်ဖိုးက ဘယ်တော့မှ ပြောင်းမှာ မဟုတ်ဘူး။

Reference

  • While Loops — loop အမျိုးအစားတွေရဲ့ နမူနာတွေ
  • User Input — indefinite loop တွေမှာ input() ကို ဘယ်လို သုံးလဲ

Accumulator Pattern — တဖြည်းဖြည်း စုသွားတဲ့ တွက်နည်း

Loop နဲ့ တွဲပြီး အသုံးအများဆုံး ပုံစံတစ်ခုက accumulator (စုဆောင်းသူ) ပါ။ အလုပ်လုပ်ပုံ ၃ ဆင့် —

  1. Loop မစခင် variable တစ်ခု အစကနေ စထားတယ် (ဥပမာ total = 0)။
  2. Loop တစ်ပတ်ချင်းစီမှာ အဲဒီ variable ကို တိုးသွားအောင် လုပ်တယ် (total += price)။
  3. Loop ပြီးမှ နောက်ဆုံး စုစုပေါင်းကို သုံးတယ်။

အပေါ်က ဈေးဝယ်စာရင်း ဥပမာမှာ total က accumulator ပါ။ နောက်တစ်ခု ကြည့်မယ် — ဂဏန်းတစ်ခု ဘယ်နှစ်လုံး ပါလဲ ရေတွက်တာ။

n = int(input("Enter a positive integer: "))
digits = 0

while n > 0:
    digits += 1
    n = n // 10

print(f"Number of digits: {digits}")
Enter a positive integer: 2026
Number of digits: 4

digits က ၀ ကနေ စတယ်။ Loop တစ်ပတ်မှာ digit တစ်လုံး ရေတွက်ပြီး (digits += 1)၊ နောက် n // 10 နဲ့ နောက်ဆုံး digit ကို ဖယ်တယ်။ n ၀ ဖြစ်တဲ့အထိ လုပ်သွားတယ်။ Accumulator pattern ကို ပေါင်းလဒ်၊ အရေအတွက်၊ အများဆုံးတန်ဖိုး ရှာတာ — အားလုံးမှာ သုံးတယ်။

Reference

  • While Loops — loop နဲ့ စုစုပေါင်း တွက်တဲ့ နမူနာတွေ
  • Operators — // နဲ့ += တို့ရဲ့ အဓိပ္ပာယ်

while...else — Loop ပြီးရင် ဘာဆက်လုပ်မလဲ

while နောက်မှာ else ထည့်လို့ရတယ်။ if...else နဲ့ ပုံစံတူပေမယ့် အဓိပ္ပာယ် ကွာတယ် — ဒီ else block က loop ရဲ့ condition False ဖြစ်ပြီး ပုံမှန် ထွက်လာတဲ့အခါ run တာပါ။

number = int(input("Enter a positive number (negative to stop): "))

while number >= 0:
    print(f"You entered {number}.")
    number = int(input("Enter a positive number (negative to stop): "))
else:
    print("Loop finished. Goodbye!")
Enter a positive number (negative to stop): 5
You entered 5.
Enter a positive number (negative to stop): 12
You entered 12.
Enter a positive number (negative to stop): -1
Loop finished. Goodbye!

အနုတ်ကိန်း ရိုက်ထည့်လိုက်တာနဲ့ number >= 0 က False ဖြစ်ပြီး loop က ထွက်သွားတယ် — အဲဒီအခါ else block က run တယ်။

အရေးကြီးတဲ့ အချက် — loop ကို break နဲ့ အလယ်က ဖြတ်ထွက်သွားရင် else block က run မဟုတ်ဘူး။ else က "condition False ဖြစ်လို့ သဘာဝအတိုင်း ပြီးသွားတာ" အတွက်ပဲ။ ဒါကို နောက် section မှာ ဥပမာနဲ့ တွေ့ရမယ်။

မှတ်ချက် while...else ကို အစပိုင်း ထူးဆန်းတယ်လို့ ခံစားရနိုင်တယ်။ တကယ်တော့ "ရှာတာ တွေ့လား/မတွေ့ဘူးလား" လို အခြေအနေတွေမှာ အရမ်း အသုံးဝင်တယ် — အောက်က သစ်သီးရှာတဲ့ ဥပမာမှာ တွေ့ရမယ်။

Reference

  • While Loops — while...else ရဲ့ တရားဝင် ရှင်းလင်းချက်
  • Built-in Functions — len() လို function တွေ loop နဲ့ တွဲသုံးတာ

break — Loop ကို အလယ်က ရပ်ခြင်း

တခါတလေ condition False ဖြစ်တာကို မစောင့်ဘဲ loop ကို အလယ်က ရပ်ချင်တယ်။ ဥပမာ စာရင်းထဲမှာ ရှာနေတဲ့ပစ္စည်း တွေ့သွားပြီဆိုရင် ဆက်ရှာနေစရာ မလိုတော့ဘူး။ ဒီအတွက် break ကို သုံးတယ် — break နဲ့ တွေ့တာနဲ့ loop က ချက်ချင်း ရပ်ပြီး ထွက်သွားတယ်။

သစ်သီးစာရင်းထဲမှာ သရက်သီး ပါမပါ ရှာတဲ့ ဥပမာ —

fruits = ["papaya", "banana", "pineapple", "mango", "grapes"]
i = 0

while i < len(fruits):
    if fruits[i] == "mango":
        print("Mango found!")
        break
    i += 1
else:
    print("Mango not found.")
Mango found!

"mango" ကို index ၃ မှာ တွေ့တာနဲ့ break က loop ကို ဖြတ်ထွက်လိုက်တယ် — ကျန် "grapes" ကို ဆက်မစစ်တော့ဘူး။ ပြီးတော့ break နဲ့ ထွက်သွားလို့ else block ("Mango not found.") က run မသွားဘူး — အပေါ်မှာ ပြောခဲ့တဲ့အတိုင်းပဲ။

"mango" ကို စာရင်းထဲက ဖျက်ကြည့်ပါ။ Loop က အဆုံး​ထိ လည်ပြီး condition False ဖြစ်မှ ထွက်မယ် — အဲဒီအခါ else block က run ပြီး "Mango not found." လို့ ပြပါလိမ့်မယ်။ ဒါက while...else ရဲ့ တကယ့် အသုံးဝင်ပုံပါ။

သတိပြုရန် Variable နာမည်တွေ ဂရုစိုက်ပါ။ စာရင်းကို fruits လို့ ပေးထားပြီး loop ထဲမှာ fruites လို့ မှားရေးမိရင် Python က NameError တက်ပါလိမ့်မယ်။ Loop နဲ့ စာရင်း တွဲသုံးတဲ့အခါ နာမည် တစ်သမတ်တည်း ဖြစ်ဖို့ အမြဲ စစ်ပါ။

Reference

  • While Loops — break ရဲ့ အလုပ်လုပ်ပုံ
  • Built-in Functions — len() နဲ့ စာရင်း အရှည် ယူတာ

ATM — PIN စစ်ခြင်း (အမှား ၃ ကြိမ်ထိ)

အခု လက်တွေ့နဲ့ နီးစပ်တဲ့ ဥပမာ တစ်ခု ကြည့်မယ် — ATM PIN စစ်တာ။ စည်းမျဉ်း ရှင်းတယ် — PIN မှန်ရင် ဝင်ခွင့်ပေး၊ မှားရင် ၃ ကြိမ်ထိ အခွင့်ပေးပြီး ၃ ကြိမ်လုံး မှားရင်တော့ account ကို lock ချမယ်။

correct_pin = "4567"
attempts = 0

while attempts < 3:
    entered = input("Enter your PIN: ")
    if entered == correct_pin:
        print("Correct PIN. Welcome!")
        break
    attempts += 1
    if attempts == 3:
        print("Too many wrong attempts. Your account is locked.")
    else:
        print(f"Wrong PIN. Attempt {attempts} of 3.")

မှားယွင်း run တစ်ခု (PIN ကို 2 ကြိမ်မှားပြီး 3rd မှာ မှန်သွားတဲ့ အခြေအနေ) —

Enter your PIN: 1111
Wrong PIN. Attempt 1 of 3.
Enter your PIN: 2222
Wrong PIN. Attempt 2 of 3.
Enter your PIN: 4567
Correct PIN. Welcome!

Logic ကို သေချာ လိုက်ကြည့်ပါ —

  1. PIN မှန်ရင် break နဲ့ ချက်ချင်း ထွက်တယ်။ attempts တိုးမှာ မဟုတ်ဘူး။
  2. PIN မှားမှ attempts ကို ၁ တိုးတယ်။
  3. attempts == 3 ဖြစ်မှ — ဆိုလိုတာက ၃ ကြိမ်လုံး မှားပြီးမှ — "account is locked" လို့ ပြတယ်။

အမှား ၁ ကြိမ်၊ ၂ ကြိမ်နဲ့ lock ချမိတဲ့ bug က beginner တွေ အဖြစ်များတယ်။ "lock" ဆိုတဲ့ စာကြောင်းက attempts ၃ ဖြစ်တဲ့ အကိုင်းမှာပဲ ရှိရမယ် — တခြား အကိုင်းမှာ ရောက်နေရင် logic မှားပြီလို့ မှတ်ပါ။

မှတ်ချက် ဒီ pattern — "N ကြိမ် အခွင့်ပေး၊ အောင်ရင် break၊ N ကြိမ်ပြည့်မှ အပြစ်ပေး" — ကို login system တိုင်း၊ OTP စစ်တိုင်း၊ ဂိမ်း အခွင့်အရေး ပေးတိုင်းမှာ သုံးတယ်။ ကျက်ထားသင့်တဲ့ ပုံစံတစ်ခုပါ။

Reference

  • While Loops — loop နဲ့ break တွဲသုံးတဲ့ ပုံစံတွေ
  • User Input — PIN လို လျှို့ဝှက်အချက်အလက် တောင်းတာ

နံပါတ် ခန့်မှန်းဂိမ်း

while loop ရဲ့ ပျော်စရာ အသုံးတစ်ခုက ဂိမ်းပါ။ ကွန်ပျူတာက ၁ ကနေ user ပေးတဲ့ အများဆုံးဂဏန်းအထိ လျှို့ဝှက်နံပါတ် တစ်ခု ရွေးထားမယ်။ User က မှန်တဲ့အထိ ခန့်မှန်းရမယ်။ random module က randint() ကို သုံးတယ်။

import random

max_value = int(input("Enter a max number: "))
secret = random.randint(1, max_value)
guesses = 0

guess = 0
while guess != secret:
    guess = int(input(f"Enter your guess (1 to {max_value}): "))
    guesses += 1
    if guess == secret:
        print(f"You won in {guesses} guesses!")
    elif guess < secret:
        print("Too low. Try again!")
    else:
        print("Too high. Try again!")
Enter a max number: 20
Enter your guess (1 to 20): 10
Too low. Try again!
Enter your guess (1 to 20): 15
Too high. Try again!
Enter your guess (1 to 20): 13
You won in 3 guesses!

ဒါ indefinite loop ရဲ့ နမူနာကောင်းပါ — ဘယ်နှစ်ခါ ခန့်မှန်းရမလဲ ကြိုမသိဘူး။ Loop ရဲ့ condition guess != secret က မှန်တဲ့အထိ True ဖြစ်နေမယ်။ အထဲမှာ if/elif/else နဲ့ hint ပေးတာ — Chapter 4 ရဲ့ condition တွေကို loop အထဲမှာ သုံးထားတာကို သတိထားပါ။

Guessing game: loop until the guess is right generate secret number ask for guess guess == secret? No hint: too high / too low try again Yes You won!
နံပါတ်မှန်းဂိမ်းရဲ့ flowchart

Reference

ဒဿမကိန်းမှ Binary သို့ (အကျဉ်းချုပ်)

while loop ရဲ့ နောက်ထပ် ဂန္တဝင် ဥပမာ တစ်ခုက ဒဿမကိန်းကို binary (0 နဲ့ 1) အဖြစ် ပြောင်းတာပါ။ နည်းလမ်းက — ကိန်းကို ၂ နဲ့ စား၊ ကြွင်းကို ချရေးထား၊ ရလဒ်ကို ထပ် ၂ နဲ့ စား... ကိန်း ၀ ဖြစ်တဲ့အထိ လုပ်သွားတာ။ ကြွင်းတွေကို ပြောင်းပြန် စီရင် binary ရပြီ။

number = int(input("Enter a positive integer: "))
binary = ""

while number > 0:
    digit = number % 2
    binary = str(digit) + binary
    number = number // 2

print("Binary:", binary)
Enter a positive integer: 13
Binary: 1101

binary = str(digit) + binary ဆိုတာ ကြွင်းအသစ်ကို အရှေ့ က ထည့်တာပါ — ဒါမှ နောက်ဆုံး ရလဒ်က ပြောင်းပြန် အစဉ်လိုက် ဖြစ်နေမှာပါ။ Binary အကြောင်း အသေးစိတ် သိချင်ရင် Appendix A မှာ အပြည့်အစုံ ဖတ်နိုင်ပါတယ်။

Reference

  • While Loops — loop နဲ့ တွက်ချက်မှု ပေါင်းစပ်ပုံ
  • Operators — % (modulo) နဲ့ // (floor division) တို့ရဲ့ အဓိပ္ပာယ်

Mini Project — ATM စနစ်

အခု သင်ခဲ့သမျှကို ပေါင်းပြီး ATM program အသေးစား တစ်ခု ရေးကြည့်မယ်။ လိုအပ်ချက်တွေ —

  • PIN 4567၊ အကောင့်ထဲမှာ ဘတ် ၁၀,၀၀၀ ရှိတယ်။
  • PIN မှား ၃ ကြိမ်ဆို account lock — program ရပ်။
  • PIN မှန်ရင် menu ပြမယ် — ငွေသွင်း (deposit)၊ ငွေထုတ် (withdraw)၊ ထွက် (quit)။
  • q ရိုက်ရင် program ရပ်။ ငွေမလောက်ဘဲ ထုတ်ရင် ရပ် (သတိပေးပြီး menu ပြန်ပြ)။
correct_pin = "4567"
balance = 10000
attempts = 0
logged_in = False

while attempts < 3:
    entered = input("Enter your PIN: ")
    if entered == correct_pin:
        print("Correct PIN. Welcome!")
        logged_in = True
        break
    attempts += 1
    print(f"Wrong PIN. Attempt {attempts} of 3.")

if not logged_in:
    print("Your account is locked. Please contact the bank.")
else:
    while True:
        choice = input("Enter d (deposit), w (withdraw), q (quit): ")
        if choice == "q":
            print("Thank you. Goodbye!")
            break
        elif choice == "d":
            amount = int(input("Deposit amount: "))
            balance += amount
            print(f"Deposited {amount} baht. Balance: {balance} baht.")
        elif choice == "w":
            amount = int(input("Withdraw amount: "))
            if amount > balance:
                print("Not enough money in the account.")
            else:
                balance -= amount
                print(f"Withdrew {amount} baht. Balance: {balance} baht.")
        else:
            print("Invalid choice. Try again.")

Sample run —

Enter your PIN: 4567
Correct PIN. Welcome!
Enter d (deposit), w (withdraw), q (quit): d
Deposit amount: 2000
Deposited 2000 baht. Balance: 12000 baht.
Enter d (deposit), w (withdraw), q (quit): w
Withdraw amount: 5000
Withdrew 5000 baht. Balance: 7000 baht.
Enter d (deposit), w (withdraw), q (quit): w
Withdraw amount: 9000
Not enough money in the account.
Enter d (deposit), w (withdraw), q (quit): q
Thank you. Goodbye!

ဒီ program မှာ အပိုင်း ၂ ပိုင်း ရှိတယ်။ ပထမ loop က PIN စစ်တာ (အမှား ၃ ကြိမ်ထိ)။ ဒုတိယ loop က menu — while True (အမြဲ True ဆိုတော့ ထာဝရ လည်မယ့် loop) ဖြစ်ပြီး အထဲက break ၂ နေရာနဲ့ ထိန်းထားတယ်။ q ရိုက်မှ menu loop က ထွက်တယ်။ ငွေထုတ်တဲ့အခါ amount > balance စစ်တာက nested if — Chapter 4 ရဲ့ အသိပညာကို loop အထဲမှာ ပြန်သုံးထားတာပါ။

သတိပြုရန် while True ဆိုတာ ရည်ရွယ်ချက်ရှိရှိ ရေးတဲ့ infinite loop ပါ — အထဲမှာ break ရှိမှ သုံးရတယ်။ break မပါဘဲ while True ရေးမိရင် program က ဘယ်တော့မှ မရပ်ဘူး။ ဒီ pattern ကို menu program တိုင်းမှာ တွေ့ရမယ်။

Reference

  • While Loops — while True + break menu pattern
  • Operators — လက်ကျန် နှိုင်းယှဉ်စစ်ဆေးမှုတွေ

Loop ရေးတဲ့အခါ သတိထားရမယ့် အမှား ၃ ခု

၁။ Infinite loop — condition ပြောင်းဖို့ မေ့တာ

count = 1
while count <= 5:
    print(f"Round {count}")
    # count += 1 ကို မေ့သွားတယ်!

count က ၁ မှာပဲ ရပ်နေတော့ condition က ထာဝရ True — program က ဘယ်တော့မှ မရပ်ဘူး။ Ctrl + C နဲ့ ရပ်ရတယ်။ Loop ရေးတိုင်း update စာကြောင်း ပါမပါ စစ်ပါ။

၂။ Off-by-one — တစ်လုံး လွဲတာ

count = 1
while count < 5:
    print(f"Round {count}")
    count += 1

ဒါက Round 1–4 ပဲ print တယ် (၄ ပတ်)။ ၅ ပတ် လိုချင်ရင် count <= 5 သုံးရမယ်။ < နဲ့ <= ရွေးတာ loop ဘယ်နှစ်ပတ် လည်မလဲ ဆုံးဖြတ်တယ် — ရည်ရွယ်ချက်နဲ့ ကိုက်မကိုက် တစ်ပတ်စာ လက်နဲ့ လိုက်တွက်ကြည့်ပါ (dry run)။

၃။ Indefinite loop မှာ input အသစ် တောင်းဖို့ မေ့တာ

price = int(input("Enter price (0 to finish): "))
while price != 0:
    total += price
    # နောက် input တောင်းဖို့ မေ့သွားတယ်!

User ပထမ ရိုက်တဲ့ ဈေးက ၀ မဟုတ်ရင် loop က ထာဝရ လည်နေမယ် — ဘာလို့လဲဆိုတော့ price က ဘယ်တော့မှ ပြောင်းမှာ မဟုတ်လို့ပါ။ Indefinite loop တိုင်းမှာ loop အထဲက condition variable ကို ပြောင်းလဲပေးတဲ့ စာကြောင်း ရှိရမယ်။

မှတ်ချက် Loop တစ်ခု ရေးပြီးတိုင်း စက္ကူပေါ်မှာ (ဒါမှမဟုတ် စိတ်ထဲမှာ) တစ်ပတ်​ချင်း လိုက်တွက်ကြည့်တဲ့ အကျင့်လုပ်ပါ။ ဒါကို dry run လို့ ခေါ်တယ်။ ၂ မိနစ် အချိန်ပေးရင် နာရီချီ debug လုပ်ရမယ့် အမှားတွေ ကြိုတွေ့နိုင်တယ်။

while Loop ကို Dry Run လုပ်ခြင်း

Loop ကို တကယ် နားလည်ဖို့ အကောင်းဆုံးနည်းက လက်နဲ့ တစ်ပတ်​ချင်း လိုက်တွက်ကြည့်တာပါ။ Binary ဥပမာ (number = 13) ကို dry run လုပ်ကြည့်မယ်။

ပတ် number (အစ) digit = number % 2 binary (အသစ်) number // 2
၁ 13 1 "1" 6
၂ 6 0 "01" 3
၃ 3 1 "101" 1
၄ 1 1 "1101" 0

၄ ပတ်မြောက်ပြီးတော့ number က ၀ ဖြစ်သွားတယ် — while number > 0 က False ဖြစ်ပြီး loop ရပ်တယ်။ binary = "1101"။ ကိုယ့် loop ကို ဒီလို ဇယားနဲ့ လိုက်တွက်ကြည့်ရင် bug တွေ အလွယ်တကူ တွေ့နိုင်တယ်။

Reference

  • While Loops — loop trace လေ့ကျင့်ဖို့ နမူနာတွေ

လေ့ကျင့်ခန်းများ

၁။ ၁ ကနေ n အထိ ပေါင်းခြင်း — n တန်ဖိုး တစ်ခုတောင်းပြီး ၁ ကနေ n အထိ ပေါင်းတဲ့ program ရေးပါ။ Output format က အောက်ပါအတိုင်း အတိအကျ ဖြစ်ရမယ်။

Enter n: 4
1 + 2 + 3 + 4 = 10

၂။ အစဉ်လိုက်/ပြောင်းပြန် ပရင့် — integer ၂ ခု (firstInt, secondInt) ထည့်ခိုင်းပြီး firstInt ကနေ secondInt အထိ ဂဏန်းတွေ တစ်ကြောင်းတည်း print ပါ။ ပထမ ဂဏန်းက ဒုတိယထက် သေးရင် အတက်အစဉ်၊ ကြီးရင် အဆင်းအစဉ် ပြပါ။

Enter the first integer: -3
Enter the second integer: 5
Output: -3 -2 -1 0 1 2 3 4 5

၃။ စုံ digit တွေ ပေါင်းခြင်း — positive integer တစ်ခု ထည့်ခိုင်းပြီး အထဲက စုံ digit (even digit) တွေချည်း ပေါင်းထုတ်ပါ။ ဥပမာ 1243 ဆိုရင် 2 + 4 = 6။ while loop နဲ့ % 10, // 10 သုံးပြီး digit တွေ ခွဲထုတ်ပါ။

၄။ နံပါတ် ပုံစံ — အောက်ပါ output အတိအကျ ထွက်အောင် while loop နဲ့ program ရေးပါ။

1-----99
2-----98
3-----97
...
98-----2
99-----1

၅။ စကားဝှက် ၃ ကြိမ် အခွင့်အရေး — မှန်ကန်တဲ့ စကားဝှက်ကို program ထဲမှာ သတ်မှတ်ထားပါ (ဥပမာ "python123")။ User ကို စကားဝှက် ရိုက်ခိုင်းပြီး မှန်ရင် "Access granted."၊ မှားရင် ကျန်အခွင့်အရေး အရေအတွက် ပြပါ။ ၃ ကြိမ်လုံး မှားရင် "Access denied. Account locked." လို့ ပြပြီး ရပ်ပါ။