On this page
วันที่ 9 — เงื่อนไข (Conditionals)
เรียนรู้การควบคุมทิศทางของโปรแกรมด้วย if, elif, else และ logical operators
เงื่อนไข (Conditionals)
โดย default คำสั่งใน Python script จะถูกรันเรียงตามลำดับจากบนลงล่าง ถ้า processing logic ต้องการ การรันตามลำดับสามารถเปลี่ยนได้สองวิธี:
- Conditional execution: block ของคำสั่งหนึ่งตัวหรือมากกว่าจะถูกรันถ้า expression หนึ่งเป็น true
- Repetitive execution: block ของคำสั่งจะถูกรันซ้ำตราบเท่าที่ expression หนึ่งยังเป็น true ในส่วนนี้จะเรียน if, else, elif statements ตัวดำเนินการเปรียบเทียบและ logical operators ที่เรียนมาแล้วจะมีประโยชน์มากที่นี่
If Condition
ใน Python และภาษาโปรแกรมอื่น keyword if ใช้ตรวจสอบว่าเงื่อนไขเป็น true หรือไม่แล้วรัน block code จำ indentation หลัง colon:
# syntax
if condition:
this part of code runs for truthy conditionsตัวอย่าง 1:
a = 3
if a > 0:
print('A is a positive number')
# A is a positive numberจากตัวอย่างข้างต้น 3 มากกว่า 0 เงื่อนไขเป็น true และ block code ถูกรัน แต่ถ้าเงื่อนไขเป็น false เราจะไม่เห็นผลลัพธ์ เพื่อให้เห็นผลลัพธ์เมื่อเงื่อนไขเป็น false เราต้องใช้ else
If Else
ถ้าเงื่อนไขเป็น true block แรกจะถูกรัน ถ้าไม่ใช่ else block จะรัน:
# syntax
if condition:
this part of code runs for truthy conditions
else:
this part of code runs for false conditionsa = 3
if a < 0:
print('A is a negative number')
else:
print('A is a positive number')เงื่อนไขข้างต้นเป็น false ดังนั้น else block จึงถูกรัน ถ้าเงื่อนไขมีมากกว่าสองแบบเราใช้ elif
If Elif Else
ในชีวิตประจำวันเราตัดสินใจโดยตรวจสอบหลายเงื่อนไข เช่นเดียวกับชีวิต การเขียนโปรแกรมก็เต็มไปด้วยเงื่อนไข เราใช้ elif เมื่อมีหลายเงื่อนไข:
# syntax
if condition:
code
elif condition:
code
else:
codea = 0
if a > 0:
print('A is a positive number')
elif a < 0:
print('A is a negative number')
else:
print('A is zero')Short Hand
# syntax
code if condition else codea = 3
print('A is positive') if a > 0 else print('A is negative') # first condition met, 'A is positive' will be printedNested Conditions
เงื่อนไขสามารถซ้อนกันได้:
# syntax
if condition:
code
if condition:
codea = 0
if a > 0:
if a % 2 == 0:
print('A is a positive and even integer')
else:
print('A is a positive number')
elif a == 0:
print('A is zero')
else:
print('A is a negative number')เราหลีกเลี่ยง nested condition ได้โดยใช้ logical operator and:
If Condition และ Logical Operators
# syntax
if condition and condition:
codea = 0
if a > 0 and a % 2 == 0:
print('A is an even and positive integer')
elif a > 0 and a % 2 != 0:
print('A is a positive integer')
elif a == 0:
print('A is zero')
else:
print('A is negative')If และ Or Logical Operators
# syntax
if condition or condition:
codeuser = 'James'
access_level = 3
if user == 'admin' or access_level >= 4:
print('Access granted!')
else:
print('Access denied!')💻 แบบฝึกหัด — วันที่ 9
ระดับ 1
- รับ input อายุจากผู้ใช้ด้วย input("Enter your age: ") ถ้าผู้ใช้อายุ 18 ปีขึ้นไปให้แสดง: You are old enough to drive. ถ้าต่ำกว่า 18 ให้แสดงว่าต้องรออีกกี่ปี
Enter your age: 30
You are old enough to learn to drive.
Enter your age: 15
You need 3 more years to learn to drive.- เปรียบเทียบค่า my_age กับ your_age ด้วย if…else ใครอายุมากกว่ากัน? ใช้ input("Enter your age: ") รับอายุ ใช้ nested condition เพื่อ print 'year' ถ้าต่างกัน 1 ปี 'years' ถ้าต่างกันมากกว่า และข้อความ custom ถ้าอายุเท่ากัน
Enter your age: 30
You are 5 years older than me.- รับตัวเลขสองตัวจากผู้ใช้ ถ้า a มากกว่า b ให้แสดง a is greater than b ถ้า a น้อยกว่า b ให้แสดง a is smaller than b ไม่งั้นให้แสดง a is equal to b
Enter number one: 4
Enter number two: 3
4 is greater than 3ระดับ 2
- เขียนโค้ดที่ให้เกรดนักเรียนตามคะแนน:
90-100, A
80-89, B
70-79, C
60-69, D
0-59, F- รับเดือนจาก input แล้วเช็คว่าเป็นฤดูอะไร:
September, October หรือ November = Autumn
December, January หรือ February = Winter
March, April หรือ May = Spring
June, July หรือ August = Summer
- List ต่อไปนี้มีผลไม้:
fruits = ['banana', 'orange', 'mango', 'lemon']ถ้าผลไม้ไม่มีใน list ให้เพิ่มแล้ว print list ที่แก้ไขแล้ว ถ้ามีอยู่แล้วให้ print: That fruit already exist in the list
ระดับ 3
- Person dictionary ต่อไปนี้ (แก้ไขได้ตามต้องการ):
person={
'first_name': 'Asabeneh',
'last_name': 'Yetayeh',
'age': 250,
'country': 'Finland',
'is_married': True,
'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address': {
'street': 'Space street',
'zipcode': '02210'
}
}- เช็คว่า person dictionary มี key skills หรือไม่ ถ้ามีให้ print middle skill ใน skills list
- เช็คว่า person มี skill 'Python' หรือไม่แล้ว print ผลลัพธ์
- ถ้า skills มีแค่ JavaScript กับ React ให้ print 'He is a front end developer' ถ้ามี Node, Python, MongoDB ให้ print 'He is a backend developer' ถ้ามี React, Node และ MongoDB ให้ print 'He is a fullstack developer' ไม่งั้นให้ print 'unknown title'
- ถ้า person แต่งงานแล้วและอยู่ใน Finland ให้ print ข้อมูลในรูปแบบ: Asabeneh Yetayeh lives in Finland. He is married.