The Center of learning aids to develop Thai education to international

50th Anniversary

การเขียนโปรแกรมด้วย Python (Multiple Conditions)

การเขียนโปรแกรมด้วย Python กับ LEGO Education คำสั่งน่ารู้: Multiple Conditions

 

เมื่อคุณใช้คำสั่ง if / elif / else เพื่อตรวจสอบหลายเงื่อนไข โปรแกรมจะทำงานตามชุดคำสั่งของเงื่อนไขใดเงื่อนไขหนึ่งเท่านั้น เนื่องจากเงื่อนไขเหล่านี้เป็นแบบ Mutually Exclusive จากตัวอย่างก่อนหน้า จะเห็นว่าเมื่อColor Sensor ตรวจพบสีแดง การกดปุ่มด้านซ้ายบน Hub จะไม่ส่งผลต่อการทำงานของโปรแกรม หากต้องการตรวจสอบหลายเงื่อนไขอย่างแท้จริง จำเป็นต้องตรวจสอบเงื่อนไขเหล่านั้นในเวลาเดียวกัน เช่นเดียวกับการสร้างชุดคำสั่ง Word Blocks หลายชุดให้ทำงานพร้อมกัน ในภาษา Python คุณสามารถเรียกใช้หลายโปรแกรมพร้อมกันได้ โดยใช้ฟังก์ชัน run() จากโมดูล runloop ก่อนหน้านี้ คุณได้เรียกใช้ฟังก์ชัน run() โดยกำหนดให้ main() เป็น Argument เพียงตัวเดียว แต่คุณสามารถกำหนดโปรแกรมหลายตัวเป็น Argument ได้ โดยใช้เครื่องหมายจุลภาค (,) คั่นระหว่างแต่ละโปรแกรม

ตัวอย่างเช่น โปรแกรมด้านล่างนี้จะแยกชุดคำสั่งสำหรับตรวจสอบสีที่เซนเซอร์ตรวจพบ และชุดคำสั่งสำหรับตรวจสอบการกดปุ่มออกเป็น 2 โปรแกรม โดยฟังก์ชัน run() ในบรรทัดสุดท้ายของโปรแกรมนั้นจะเริ่มการทำงานของทั้งสองโปรแกรมพร้อมกัน

 

from hub import button, port, sound
import color
import color_sensor
import runloop

# This function returns `True` if the Color Sensor detects red.
def red_detected():
    return color_sensor.color(port.A) == color.RED

# This function returns `True` if the left button is pressed.
def left_pressed():
    return button.pressed(button.LEFT) > 0

# This coroutine continuously checks if the Color Sensor detects red.
async def check_color():
    while True:
        # Wait until red is detected.
        while not red_detected():
            await runloop.sleep_ms(1)
        # When it's detected start a very long beep.
        sound.beep(440, 1000000, 100)
        # Wait until red is no longer detected.
        while red_detected():
            await runloop.sleep_ms(1)
        # When red is no longer detected stop the sound.
        sound.stop()

# This coroutine continuously checks if the left button is pressed.
async def check_button():
    while True:
        # Wait until the left button is pressed.
        while not left_pressed():
            await runloop.sleep_ms(1)
        # When it's pressed make a short beep.
        sound.beep(880, 200, 100)
        # Wait until the left button is released.
        while left_pressed():
            await runloop.sleep_ms(1)

# Run both coroutines.
runloop.run(check_color(), check_button())

 

ลองนำชิ้นส่วนเลโก้สีแดงมาเคลื่อนผ่านด้านหน้า Color Sensor และกดปุ่มด้านซ้ายบน Hub ขณะที่โปรแกรมกำลังทำงานเช่นเดียวกับตัวอย่างก่อนหน้านี้ คุณจะได้ยินเสียงบี๊บดัง อย่างต่อเนื่องตราบใดที่เซนเซอร์ยังตรวจพบสีแดง และจะได้ยินเสียงบี๊บสั้น ๆ ทุกครั้งที่กดปุ่มด้านซ้าย แต่ในครั้งนี้คุณสามารถกดปุ่มด้านซ้าย และได้ยินเสียงบี๊บได้ แม้ว่าเซนเซอร์กำลังตรวจพบสีแดงอยู่ก็ตาม เนื่องจากทั้งสองฟังก์ชันสามารถทำงานพร้อมกันได้

ข้อควรจำ เมื่อสร้างโปรแกรม (Coroutines) ด้วยตนเอง :

  • ภายในโปรแกรมควรมีการใช้คำสั่ง await อย่างน้อยหนึ่งคำสั่ง
  • เมื่อใช้ลูป while ที่ทำงานวนซ้ำอย่างรวดเร็ว และต่อเนื่อง (Tight Loop) ควรเพิ่มคำสั่ง await runloop.sleep_ms(1) ภายในลูป เพื่อให้โปรแกรมอื่น ๆ สามารถเริ่มต้น และทำงานได้

 

Writer : Pacharee Wongcherngyoot
CODE : 80D45678

฿ 34,500