Selection and conditionals
แนะนำใหม่
6:37
พื้นฐานการเขียนโปรแกรม
Every program in this course is complete and runnable: type it, run it, then change one line. The whole skill is a loop. First predict what the code will do…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| program/ˈprəʊɡræm/ | A set of instructions specifying operations for a computer to carry out. |
| condition/kənˈdɪʃn/ | เงื่อนไข |
| comparisons | |
| selection/sɪˈlekʃn/ | การเลือก |
| boundary/ˈbaʊndəri/ | A line, surface or limit that separates regions or marks the extent of an area or set. |
| input/ˈɪnpʊt/ | A value, signal or resource supplied to a function, program, device or process. |
| syntax error/ˈsɪntæks ˈerə/ | A mistake that breaks the grammatical rules of a programming language. |
ทำไมผู้สูงอายุถึงจ่ายราคาสำหรับนักเรียน?
- โรงภาพยนตร์คิดเงิน £5 สำหรับเด็กและผู้ใหญ่อายุเกิน65ปี, £7 สำหรับนักเรียน และ £10 สำหรับผู้อื่น ผู้เรียนอายุ70 ปีถูกเก็บเงิน £7
- กฎทุกข้อในโปรแกรมถูกต้อง แต่ถูกทดสอบตามลำดับที่ผิด บทเรียนนี้เกี่ยวกับวิธีที่โปรแกรมเลือกเส้นทาง และทำไมลำดับจึงเป็นส่วนหนึ่งของการออกแบบ
เงื่อนไขคือคำถามที่มีคำตอบ True หรือ False
- เงื่อนไข คือนิพจน์whose value เป็น
bool:age >= 65,answer == "y",n != 0การเปรียบเทียบได้แก่<,<=,>,>=,==(เท่ากับ) และ!=(ไม่เท่ากับ) - เชื่อมต่อเงื่อนไขด้วย
and(ต้องเป็นจริงทั้งคู่),or(อย่างน้อยหนึ่งเป็นจริง) และnot(สลับค่า)3 >= 3 and not (2 > 5)มีค่าเป็นTrue
student = input("Student? y/n ") == "y" # already a bool
print(type(student)) # <class 'bool'>
What does print(3 >= 3 and not (2 > 5)) show?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
3 >= 3 เป็น True; 2 > 5 เป็น False, so not (2 > 5) เป็น True. Both sides are true, so and gives True.
if และ else: เลือกเส้นทางหนึ่งจากสองเส้นทาง
if condition:รันบล็อกที่ขยับเข้าด้านในเมื่อเงื่อนไขเป็นTrueบล็อกelse:รันเมื่อเป็นFalseโดยจะรันเพียงอย่างใดอย่างหนึ่งเท่านั้น- การขยับเข้าด้านใน คือ บล็อก บรรทัดที่ขยับเข้าด้านในภายใต้
ifเป็นส่วนหนึ่งของมัน; บรรทัดแรกกลับไปยังขอบซ้ายจะรันตามสิ่งที่เกิดขึ้น
if total >= 50:
fee = 0
else:
fee = 5
print("Delivery:", fee) # always runs
A line that is not indented, written straight after an if block, runs only when the condition is True.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Only indented lines belong to the if. The first line back at the left edge is outside the block, so it runs either way.
elif: ทางเลือกที่เป็นจริงครั้งแรกจะชนะ
- การเลือก ด้วย
if … elif … elseทดสอบเงื่อนไขจากบนลงล่างและรันเฉพาะ ทางเลือกแรก ที่เป็นTrueทางเลือกที่เหลือทั้งหมดจะถูกข้าม แม้ว่าจะเป็นจริงก็ตาม elseจะจับทุกสิ่งที่เหลืออยู่ ดังนั้นลูกโซ่ที่สมบูรณ์จะใช้เวลาเส้นทางเดียวเสมอ


A shop uses if total >= 50: fee = 0, then elif total >= 20: fee = 3, then else: fee = 5. What is fee when total is exactly 20?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
20 >= 50 เป็น False, so the chain moves on; 20 >= 20 เป็น True, so fee = 3 runs and the else is skipped. A boundary value is exactly the case worth testing.
เชื่อในกระบวนการตกผ่าน (fall-through)
- การเข้าถึง
elifจะเกิดขึ้นก็ต่อเมื่อการทดสอบทั้งหมดที่อยู่เหนือมันล้มเหลวเท่านั้น ดังนั้นelif score >= 70จึงไม่จำเป็นต้องมีand score < 80: การถึงบรรทัดนั้นพิสูจน์แล้วว่าคะแนนต่ำกว่า 80 แล้ว - การเขียนการทดสอบเพิ่มเติมมีความยาวมากขึ้น และการทำซ้ำแต่ละครั้งคือตำแหน่งที่อาจเกิดข้อผิดพลาดที่ขอบเขตเพิ่มขึ้นอีกหนึ่งจุด
if score >= 80:
band = "A"
elif score >= 70: # here score < 80 is already known
band = "B"
elif score >= 60:
band = "C"
else:
band = "F"
ветвь (branch) ใดจะถูกรัน?
Drag the score and watch the chain test each condition from the top. Only the first true branch runs; at 79 the test score >= 80 fails and score >= 70 wins.
In the band chain, why does elif score >= 70: not need and score < 80?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
แต่ละ elif inherits the failure of every test above it. A score of 80 or more has already been caught by the first branch.
วางกฎที่แคบกว่าไว้ก่อน
- เมื่อมีสองกฎที่ทั้งสองเป็นจริงไปได้ กฎที่ถูกเขียนไว้ก่อนจะเป็นผู้ตัดสิน ให้วางกฎที่ต้องชนะ (โดยทั่วไปคือกฎที่แคบกว่า) ไว้เหนือกฎที่กว้างกว่า
- ทดสอบอินพุตหนึ่งค่าจากแต่ละเส้นทาง และทดสอบอินพุตหนึ่งค่าที่ตรงกับ สอง กฎ กรณีซ้อนทับนี้เป็นจุดที่ความผิดพลาดด้านลำดับซ่อนอยู่
- เด็กและผู้ที่มีอายุเกิน 65 เสีย £5, นักศึกษาเสีย £7, คนอื่นเสีย £10.
- อายุก่อน:
if age < 12 or age >= 65:แล้วelif student:แล้วelse:. นักศึกษาอายุ 70 ปี เสีย £5 - นักศึกษาก่อน: บุคคลเดียวกันจะเข้าถึง
if student:และเสีย £7. โปรแกรมทั้งสองผ่านการทดสอบด้วยนักศึกษาอายุ 20 ปี (£7) และบุคคลอายุ 30 ปี (£10) กรณีซ้อนทับนี้จึงเป็นจุดที่แสดงความแตกต่าง
Put the lines in order so that a 70-year-old student pays £5 and a 20-year-old student pays £7.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The age rule must win the overlap case, so it is tested first. A 20-year-old student fails the age test and reaches elif student:.
รวมเงื่อนไขด้วยความระมัดระวัง
- Python อนุญาตให้ใช้การเปรียบเทียบแบบต่อเนื่อง:
0 <= mark <= 100หมายถึง0 <= mark and mark <= 100 andจะหยุดทันทีที่ทราบคำตอบแล้วn != 0 and total / n > 5ไม่เคยหารด้วยศูนย์ เพราะเมื่อnเป็น0ด้านซ้ายของสมการจะเป็นFalseไปแล้ว
Let x = 15. Which conditions are truthy (would be treated as true in an if statement)? Select all that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The chain holds, and $15$ is a multiple of 5. not x > 10 เป็น False. The last one is the trap: it means (x == 10) or 20, and 20 counts as true, so it is true for every x.
คะแนนที่หลุดหายไป
=ในเงื่อนไข.if x = 5:เป็นข้อผิดพลาดทางไวยากรณ์; การเปรียบเทียบต้องใช้==x == 1 or 2. นั่นหมายความว่า(x == 1) or 2, และ2นับเป็นจริง ดังนั้นการทดสอบจึง เป็นจริงเสมอ เขียนx == 1 or x == 2หรือx in (1, 2)- แยก
ifที่คุณตั้งใจจะใช้elif. คำสั่งifแยกกันสามคำสั่งสามารถทำงานได้พร้อมกัน แต่การใช้elifแบบต่อเนื่องจะทำงานเพียงครั้งเดียว - ขอบเขต
>หรือ>=? ทดสอบที่ค่าขอบเขตที่แน่นอน เช่น คะแนนที่เท่ากับ 70 พอดี
A program has three separate statements: if score >= 60: print("C"), if score >= 70: print("B"), if score >= 80: print("A"). What does it print for a score of 85?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Separate if statements are each tested, so all three run. An if … elif chain with the highest band first would print only A.
The cinema code passes tests for a 30-year-old, a 20-year-old student and an 8-year-old. Which extra test would reveal that the student rule was put first?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Only a customer who matches two rules shows which rule wins. A 70-year-old student should pay £5; with the student rule first, they pay £7.
คุณเข้าใจแล้ว
- เงื่อนไข คือ
boolexpression; สร้างขึ้นจากการเปรียบเทียบโดยใช้and,orและnot if … elif … elseทดสอบจากด้านบนและรันเฉพาะ สาขาที่เป็นจริงครั้งแรก; ส่วนelseจะรับสิ่งที่เหลืออยู่- แต่ละ
elifทราบอยู่แล้วว่าการทดสอบที่อยู่เหนือมันล้มเหลว ลำดับคือการออกแบบ: วางกฎที่ต้องชนะไว้ก่อน และทดสอบกรณีซ้อนทับและกรณีขอบเขต
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
You've got it
- A condition 条件 is a
boolexpression; build it from comparisons withand,orandnot. if … elif … elsetests from the top and runs only the first true branch;elsetakes what is left.- Each
elifalready knows that the tests above it failed. Order is design: put the rule that must win first, and test the overlap and boundary cases.
| Customer | Age test first | Student test first |
|---|---|---|
| 30-year-old | £10 | £10 |
| 20-year-old student | £7 | £7 |
| 70-year-old student | £5: correct | £7: wrong |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้