Making decisions
Making a choice
ifruns some code only when a condition is true.- The condition is followed by a colon
:. - The lines that belong to the
ifare indented (4 spaces).
การเลือกเงื่อนไข
ifจะรันโค้ด ก็ต่อเมื่อ เงื่อนไขเป็นจริงเท่านั้น- ตามเงื่อนไขจะมีจุดสองจุด
: - บรรทัดที่เป็นส่วนหนึ่งของ
ifจะต้อง ย่อหน้า (4 ช่องว่าง)
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
การเปรียบเทียบ
- เงื่อนไขมักจะเป็นการเปรียบเทียบที่เป็น True หรือ False
==เท่ากับ,!=ไม่เท่ากับ,<><=>=- หมายเหตุ:
==ทดสอบ ความเท่ากัน; ตัว=เดียว จัดเก็บ ค่า
temperature = 30
if temperature > 25:
print("It is hot.")
if / elif / else
elif("else if") checks another condition if the first was false.elseruns when none of the conditions were true.- Python checks them in order and stops at the first true one.
if / elif / else
elif("else if") จะตรวจสอบเงื่อนไขอื่นหากเงื่อนไขแรกเป็น falseelseรันเมื่อ ไม่มี เงื่อนไขใดเป็นจริงเลย- Python จะตรวจสอบตาม ลำดับ และหยุดที่ตัวที่เป็น true ตัวแรก
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
The remainder
a % bis what is left over whenais divided byb.year % 4 == 0means the year divides by 4 exactly — nothing left over.8 % 3is2, because 8 is two 3s with 2 left over.
ส่วนเหลือ
a % bคือสิ่งที่ เหลืออยู่ เมื่อaหารด้วยbyear % 4 == 0หมายความว่าปีนั้นหารด้วย 4 ลงตัว — ไม่มีเศษเหลือ8 % 3คือ2เพราะ 8 เป็น 3 สองตัว มีเศษเหลือ 2
Combine conditions
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
รวมเงื่อนไข
andเป็นจริงก็ต่อเมื่อ ทั้งสอง ด้านเป็นจริงorเป็นจริงเมื่อ อย่างน้อยหนึ่ง ด้านเป็นจริงnotจะกลับจาก true เป็น false และ false เป็น true
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
ใน伪代码 Cambridge
- Python
if/elif/elseตรงกับการสอบIF … ELSE … ENDIFและCASE
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
CASE OF Grade
"A": OUTPUT "Top"
"B": OUTPUT "Good"
OTHERWISE OUTPUT "Keep trying"
ENDCASE
Common mistakes
- Compare with
==; a single=assigns. - Write
elif, notelse if, and end eachif/elif/elseline with:. - The indented block below
ifis what runs when the condition is true.
ข้อผิดพลาดที่พบบ่อย
- เปรียบเทียบกับ
==; ตัว=เดียวคือการกำหนดค่า - เขียน
elifไม่ใช่else ifและลงท้ายแต่ละบรรทัดif/elif/elseด้วย: - บล็อกที่ถูกย่อหน้าอยู่ใต้
ifคือส่วนที่จะ被执行เมื่อเงื่อนไขเป็นจริง
Now you try
- When you Run, type the input in the box under the code and press Enter.
- The checks type the input for you. Read it, then decide what to print.
- Match the output text exactly.
ลองดูเลย
- เมื่อคุณ Run ให้พิมพ์ข้อมูลในช่องใต้โค้ดแล้วกด Enter
- การตรวจสอบจะพิมพ์ข้อมูลเข้าให้คุณ อ่านแล้วตัดสินใจว่าจะพิมพ์อะไร
- ตรงกับข้อความผลลัพธ์ อย่างถูกต้อง
if / elif / else
Conditions are checked top-down; only the first true branch runs.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Read a score. Print A if it is 80+, B if it is 60+, otherwise C. Use if / elif / else. For input 75, print B.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Read an age. Print Yes if the age is from 13 to 19 (a teenager), otherwise No. Use and. For input 16, print Yes.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Read a year with int(input()). A year divides by 4 when the remainder is 0: year % 4 == 0. Print Leap if it divides by 4, otherwise Common. For the input 2024, print Leap.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Dividing by 4 is not the whole rule. A year that also divides by 100 is not a leap year, unless it divides by 400 too. 1900 is the trap: it divides by 4 and by 100, so print Common. Print Leap or Common.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่