Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Switch Statement: คู่มือเขียน Switch Case ใน Python

ไม่เหมือน Java หรือ C# Python ไม่มี built-in switch statement หมายถึงไม่สามารถ evaluate switch expression ได้โดยไม่เขียนโค้ดเลียนแบบ "switch...case" เอง

คู่มือนี้อธิบายวิธีเขียน "switch...case" ใน Python พร้อม 2 ตัวอย่าง faux-switch statement ช่วยเรียนรู้เขียนในโค้ดของตัวเอง

Switch Statements: บทนำสรุป

Switch statement ให้เลือกหนึ่งใน code blocks ชุดหนึ่งเพื่อรัน เป็นวิธี evaluate multiple expressions ในโปรแกรม

Switch statement ทำงานโดย evaluate switch statement และเปรียบเทียบผลลัพธ์กับค่าใน "case" statements ถ้าพบ match จะรัน block โค้ดที่สัมพันธ์ ไม่งั้นไม่เกิดอะไรขึ้น

Optional: "default" keyword ใช้รัน block โค้ดถ้าไม่มี statement ไหน match กับ expression นั้น

Python Switch Statement: คืนค่า Individual Values

เขียนโปรแกรมแปลง numerical value เป็นวันในสัปดาห์

เริ่มถามผู้ใช้ตัวเลขที่ต้องการแปลงเป็นวัน:

convert_to_day = int(input("Insert a day of the week: "))

แปลงค่าที่ผู้ใช้ใส่เป็น integer จะอธิบายทำไมต้องทำนี้ทีหลัง

กำหนด dictionary มีวันทั้งหมดในสัปดาห์ แต่ละ key เก็บวันเป็นตัวเลข แต่ละ value เก็บวันเขียนออกมา (เช่น Tuesday):

days = {
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday",
    6: "Saturday",
    7: "Sunday"
}

มี 7 keys และ values ใน dictionary key "1" แทน "Monday" จนถึงวันสุดท้าย

ใช้ dictionary get() method เข้าถึง item ใน dictionary ที่สัมพันธ์กับค่าที่ผู้ใช้ใส่:

day_as_written = days.get(convert_to_day, "There is no day with this numerical value.")
print(day_as_written)

แปลงค่าที่ผู้ใช้ใส่เป็น integer เพื่อใช้ใน get() statement ถ้าไม่มี key เท่ากับค่า "convert_to_day" โค้ดคืน "There is no day with this numerical value."

ถ้าผู้ใช้ใส่ "1" โค้ดจะ evaluate:

days[1]

จะดึง value ที่สัมพันธ์กับ key "1" ในลิสต์

รันโค้ด:

Insert a day of the week: 1
Monday

โค้ดแปลงตัวเลขเป็น string สำเร็จ ลองรันวันที่ไม่มีใน dictionary:

Insert a day of the week: 8
There is no day with this numerical value.

โค้ดทำงานแม้ผู้ใช้ใส่ค่า invalid

Python Switch Statement: เรียก Functions (Calling Functions)

ใช้ syntax นี้เรียก functions ในโค้ดได้ สร้าง application แสดงข้อมูลลิสต์ purchases ที่ร้านแซนด์วิช

ก่อน กำหนดลิสต์ purchases:

purchases = [2.50, 2.50, 2.75, 3.90, 5.60, 2.40]

กำหนด 3 functions คำนวณ:

  • จำนวน purchases ในลิสต์
  • ค่าเฉลี่ย purchase value
  • purchase ใหญ่ที่สุด

กำหนด functions:

def number_of_purchases():
    total = len(purchases)
    print(total)
def average_value():
    average = sum(purchases) / len(purchases)
    print(average)
def largest_purchase():
    largest = max(purchases)
    print(largest)

พร้อม functions แล้ว เริ่มเขียน faux-switch statement เหมือนตัวอย่างแรก เริ่มถามผู้ใช้ใส่ค่า ค่าที่ใส่ควรสัมพันธ์กับ function หนึ่งใน functions

print("[1] Display the number of purchases made today")
print("[2] Display the average value of all the purchases made today")
print("[3] Display the largest purchase made today")
to_run = int(input("What would you like to do? "))

โค้ดพิมพ์ 3 ข้อความ แต่ละอันแจ้ง option ที่เลือกได้ โค้ดถามผู้ใช้ใส่ค่า แปลงเป็น number เพื่อใช้ต่อในโปรแกรม

กำหนด dictionary map functions กับ numbers:

options = {
    1: number_of_purchases,
    2: average_value,
    3: largest_purchase
}

Value "1" map กับ function "number_of_purchases" "2" map "average_value" "3" map "largest_purchase"

ใช้ get() method เลือก function ที่โปรแกรมควรรัน:

function_to_execute = options.get(to_run)
function_to_execute()

โค้ดดึง function จาก dictionary ที่ต้องการ execute ไม่ระบุค่าที่ 2 ใน get() เพราะต้องเรียก function เพื่อให้โค้ดทำงาน หมายถึงถ้าผู้ใช้ใส่ค่า invalid โค้ด error มีวิธี handle behavior นี้ แต่อยู่นอกขอบเขต tutorial

รันโค้ด:

[1] Display the number of purchases made today
[2] Display the average value of all the purchases made today
[3] Display the largest purchase made today
What would you like to do? 3
5.6

เลือก option 3 โค้ดพิมพ์ largest purchase ในวันนั้น สำเร็จ ลอง option 2:

[1] Display the number of purchases made today
[2] Display the average value of all the purchases made today
[3] Display the largest purchase made today
What would you like to do? 2
3.275

โค้ดคืนค่า average value ของ purchases ในลิสต์

สรุป

แม้ Python ไม่มี built-in switch statement แต่สร้างได้ด้วย dictionary และ get() method

Switch statements 有用ถ้าต้องการ evaluate expression กับ multiple potential outcomes

เขียน faux-switch statement ดึง values จาก dictionary ขึ้นกับ key ที่ระบุ หรือดึง function จาก dictionary based on key ที่อ้างอิง

ตอนนี้คุณพร้อมสร้าง switch statements ของตัวเองใน Python programming language อย่าง expert coder แล้ว!