Example 11 — Count Students in Two Clubs ตัวอย่างที่ 11 — นับนักเรียนที่สมัคร 2 ชมรม

Last updated แก้ไขล่าสุด

Compare the lists of two clubs and count the students who are on both. เทียบรายชื่อ 2 ชมรม แล้วนับคนที่อยู่ในทั้งสองรายการ

Read the problem, follow the flowchart, then press Run. You can change the names in photo_club and cooking_club to try your own data.

อ่านโจทย์ ไล่ตาม flowchart แล้วกด Run โดยแก้รายชื่อใน photo_club และ cooking_club เพื่อลองข้อมูลของตัวเองได้

1 The problem โจทย์

photo_club holds the names of the students who joined the photography club, and cooking_club holds the names of those who joined the cooking club. Every name is different. Show how many students joined both clubs.

photo_club เก็บรายชื่อนักเรียนที่สมัครชมรมถ่ายภาพ และ cooking_club เก็บรายชื่อนักเรียนที่สมัครชมรมทำอาหาร ชื่อนักเรียนไม่ซ้ำกัน จงแสดงจำนวนนักเรียนที่สมัครทั้ง 2 ชมรม

  • Example: the names in the code below → 2 (Bua and Fah)
  • ตัวอย่าง: รายชื่อในโค้ดด้านล่าง → 2 (Bua และ Fah)
Commands used in this program:
  • student in cooking_club is True when the value of student is in that list.
  • both += 1 adds 1 to the counter.
คำสั่งที่ใช้ในโปรแกรมนี้:
  • student in cooking_club เป็น True เมื่อมีค่า student อยู่ใน list นั้น
  • both += 1 เพิ่มค่าตัวนับขึ้น 1
2 Main program — flowchart and full code โปรแกรมหลัก — flowchart และโค้ดทั้งหมด

The program starts by setting the names of the two clubs in photo_club and cooking_club, then sets both = 0 as the counter of students in both clubs. Next it takes one name at a time from photo_club as student and checks whether student in cooking_club: if so, it adds 1 to both; if not, it moves on to the next name. It repeats this until every name is done, then leaves the loop, shows both with print, and the program ends.

เริ่มต้นด้วยกำหนดรายชื่อของ 2 ชมรมไว้ใน photo_club และ cooking_club แล้วกำหนด both = 0 เป็นตัวนับจำนวนนักเรียนที่สมัครทั้ง 2 ชมรม ถัดไปหยิบชื่อจาก photo_club ทีละชื่อด้วย student แล้วตรวจสอบเงื่อนไขว่า student in cooking_club หรือไม่ ถ้าใช่ให้เพิ่ม both ขึ้น 1 ถ้าไม่ใช่ให้ไปชื่อถัดไป ทำแบบนี้จนครบทุกชื่อจึงออกจาก loop แล้วแสดงค่า both ด้วย print แล้วจบการทำงานของโปรแกรม

check every name Start Set the first club photo_club = [...] Set the second club cooking_club = [...] Start the count at 0 both = 0 Name left? for student in photo_club Yes In both clubs? student in cooking_club? Yes Add 1 to the count both += 1 No No Show the count print(both) End
[main]
[output]
2
3 Results of the program ผลของโปรแกรม

Change the two lists in card 2 and press Run to check each row of this table. The third row has the same names in a different order, which shows that the order of the names does not change the count.

แก้ list ทั้งสองในการ์ดที่ 2 แล้วกด Run เพื่อตรวจแต่ละแถวของตารางนี้ แถวที่ 3 มีชื่อเดียวกันแต่ลำดับต่างกัน แสดงว่าลำดับของชื่อไม่มีผลต่อจำนวนที่นับได้

photo_club cooking_club Outputผลลัพธ์
Anan, Bua, Chai, Dao, Fah Bua, Ek, Fah, Gun, Nok 2
Anan, Bua Chai, Dao 0
Anan, Bua Bua, Anan 2
Anan [] (nobody joined)(ไม่มีคนสมัคร) 0
4 Think further ชวนคิดต่อ

Suppose the program should also show the names of the students who joined both clubs. Which line would you add?

ถ้าต้องการแสดงชื่อนักเรียนที่สมัครทั้ง 2 ชมรมด้วย ต้องเพิ่มบรรทัดอะไร

Show the answer ดูคำตอบ

Add print(student) under the if, at the same level as both += 1. That line runs only for a name that is on both lists.

เพิ่ม print(student) ใต้ if ในระดับเดียวกับ both += 1 เพราะบรรทัดนี้ทำงานเฉพาะชื่อที่อยู่ในทั้งสอง list

[names]
[output]
Bua
Fah
2

The Run buttons on this page simulate Python with JavaScript, the same as the other lessons. To run the real Python, paste the code into Google Colab.

ปุ่ม Run ในหน้านี้จำลองโค้ด Python ด้วย JavaScript เหมือนบทเรียนอื่นๆ หากต้องการรัน Python จริง ให้นำโค้ดไปวางใน Google Colab

References References / แหล่งอ้างอิง

  1. Python docs — Membership test operations (the in operator) (คำสั่ง in)
  2. Python docs — The for statement (a loop over a list) (loop ตามสมาชิกของ list)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What is the value of "Bua" in ["Anan", "Bua"]?"Bua" in ["Anan", "Bua"] ได้ค่าอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) TrueTrue

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) FalseFalse
    Wrong: "Bua" is in the list.ผิด: มี "Bua" อยู่ใน list
  • (B) TrueTrue
    Correct! in gives True when the value is in the list.ถูกต้อง! in ให้ True เมื่อพบค่าใน list
  • (C) 11
    Wrong: in gives True or False, not a position.ผิด: in ให้ค่า True หรือ False ไม่ใช่ตำแหน่ง
  • (D) "Bua""Bua"
    Wrong: in does not give back the value it looked for.ผิด: in ไม่ได้คืนค่าที่ค้นหา

ข้อ 2

2 / 10

Why is both = 0 set before the loop?
both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
ทำไมต้องกำหนด both = 0 ก่อน loop?
both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) To start counting from 0เพื่อเริ่มนับจาก 0

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) To clear the listเพื่อล้าง list
    Wrong: both is a number and has nothing to do with the lists.ผิด: both เป็นตัวเลข ไม่เกี่ยวกับ list
  • (B) To stop the loopเพื่อหยุด loop
    Wrong: the loop ends when every name has been taken.ผิด: loop จบเมื่อหยิบชื่อครบ
  • (C) Because print needs itเพราะ print ต้องการ
    Wrong: print can show any value.ผิด: print แสดงค่าใดก็ได้
  • (D) To start counting from 0เพื่อเริ่มนับจาก 0
    Correct! A counter needs a starting value before += is used.ถูกต้อง! ตัวนับต้องมีค่าเริ่มต้นก่อนใช้ +=

ข้อ 3

3 / 10

In this code, how many rounds does the loop run?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = ["Bua", "Ek", "Fah", "Gun", "Nok"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
จากโค้ดนี้ loop ทำงานกี่รอบ?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = ["Bua", "Ek", "Fah", "Gun", "Nok"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 55

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 55
    Correct! photo_club has 5 names.ถูกต้อง! photo_club มี 5 ชื่อ
  • (B) 22
    Wrong: 2 is the number of names on both lists.ผิด: 2 คือจำนวนชื่อที่พบในทั้งสอง list
  • (C) 1010
    Wrong: the loop goes over photo_club only.ผิด: loop ไล่เฉพาะ photo_club
  • (D) 2525
    Wrong: the code has only one for.ผิด: โค้ดมี for เพียงตัวเดียว

ข้อ 4

4 / 10

In this code, in how many rounds is the if condition True?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = ["Bua", "Ek", "Fah", "Gun", "Nok"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
จากโค้ดนี้ เงื่อนไข if เป็น True กี่รอบ?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = ["Bua", "Ek", "Fah", "Gun", "Nok"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 22

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 55
    Wrong: 5 is the total number of rounds.ผิด: 5 คือจำนวนรอบทั้งหมดของ loop
  • (B) 00
    Wrong: some names are on both lists.ผิด: มีชื่อที่ตรงกันอยู่ใน list ทั้งสอง
  • (C) 22
    Correct! The matching names are Bua and Fah.ถูกต้อง! ชื่อที่ตรงกันคือ Bua และ Fah
  • (D) 33
    Wrong: only Bua and Fah are on both lists.ผิด: มีแค่ Bua และ Fah ที่อยู่ในทั้งสอง list

ข้อ 5

5 / 10

If the loop goes over cooking_club and checks in photo_club instead, what happens?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = ["Bua", "Ek", "Fah", "Gun", "Nok"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
ถ้าสลับให้ loop ไล่ cooking_club แล้วเช็กใน photo_club ผลเป็นอย่างไร?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = ["Bua", "Ek", "Fah", "Gun", "Nok"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) It still gives 2ได้ 2 เหมือนเดิม

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It gives 5ได้ 5
    Wrong: 5 is the number of names on each list.ผิด: 5 คือจำนวนชื่อทั้งหมดในแต่ละ list
  • (B) It still gives 2ได้ 2 เหมือนเดิม
    Correct! The names on both lists are still the same.ถูกต้อง! ชื่อที่อยู่ในทั้งสอง list ยังเป็นชุดเดิม
  • (C) It gives 0ได้ 0
    Wrong: the matching names do not disappear.ผิด: ชื่อที่ตรงกันไม่ได้หายไป
  • (D) An error occursเกิด error
    Wrong: both variables are lists, so they can be swapped.ผิด: ทั้งสองตัวแปรเป็น list จึงใช้สลับกันได้

ข้อ 6

6 / 10

If cooking_club = [], what is the output?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = []

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
ถ้า cooking_club = [] ผลเป็นอะไร?
photo_club = ["Anan", "Bua", "Chai", "Dao", "Fah"]
cooking_club = []

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 00

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 00
    Correct! An empty list has no matching name.ถูกต้อง! list ว่างไม่มีชื่อใดตรงกันเลย
  • (B) 11
    Wrong: the condition is never true.ผิด: เงื่อนไขไม่เป็นจริงเลยสักรอบ
  • (C) An error occursเกิด error
    Wrong: in works with an empty list.ผิด: in ใช้กับ list ว่างได้
  • (D) 55
    Wrong: 5 is the number of rounds, not the count.ผิด: 5 คือจำนวนรอบ ไม่ใช่จำนวนที่นับได้

ข้อ 7

7 / 10

What is the output of this code?
photo_club = ["Anan", "Bua"]
cooking_club = ["Bua", "Anan"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
โค้ดนี้แสดงผลเป็นอะไร?
photo_club = ["Anan", "Bua"]
cooking_club = ["Bua", "Anan"]

both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 22

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 00
    Wrong: in does not care about the order of the list.ผิด: in ไม่สนใจลำดับใน list
  • (B) 11
    Wrong: both names are on the second list.ผิด: ทั้งสองชื่ออยู่ใน list ที่สอง
  • (C) An error occursเกิด error
    Wrong: a different order does not cause an error.ผิด: ลำดับที่ต่างกันไม่ทำให้เกิด error
  • (D) 22
    Correct! The order makes no difference, and both names are on both lists.ถูกต้อง! ลำดับไม่มีผล ทั้ง 2 ชื่ออยู่ในทั้งสอง list

ข้อ 8

8 / 10

Why does the problem say that the names in a list are all different?
both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
ทำไมโจทย์ต้องกำหนดว่าชื่อใน list เดียวกันไม่ซ้ำกัน?
both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Because a name repeated in photo_club would be counted more than onceเพราะชื่อซ้ำใน photo_club จะถูกนับหลายครั้ง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) To keep the lists shortเพื่อให้ list สั้น
    Wrong: the length of a list has nothing to do with repeated names.ผิด: ความยาวของ list ไม่เกี่ยวกับชื่อซ้ำ
  • (B) Because in cannot work with repeated namesเพราะ in ใช้กับชื่อซ้ำไม่ได้
    Wrong: in works normally.ผิด: in ทำงานได้ปกติ
  • (C) Because a name repeated in photo_club would be counted more than onceเพราะชื่อซ้ำใน photo_club จะถูกนับหลายครั้ง
    Correct! The loop counts every round with a match, so one student could be counted twice.ถูกต้อง! loop นับทุกรอบที่เจอชื่อตรงกัน จึงนับนักเรียนคนเดียวซ้ำได้
  • (D) To make the program fasterเพื่อให้รันเร็วขึ้น
    Wrong: the reason is a correct count.ผิด: เหตุผลคือความถูกต้องของจำนวนที่นับ

ข้อ 9

9 / 10

This code counts the students on both lists, but the line both += 1 is missing. Where should it go?
both = 0
for student in photo_club:
    if student in cooking_club:
print(both)
โค้ดนี้นับนักเรียนที่อยู่ในทั้งสอง list โดยยังขาดบรรทัด both += 1 บรรทัดนี้ควรอยู่ตรงไหน?
both = 0
for student in photo_club:
    if student in cooking_club:
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Under the ifใต้ if

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Outside the loopนอก loop
    Wrong: it would run once, whatever the names are.ผิด: จะทำงานครั้งเดียวและไม่ได้ขึ้นกับชื่อ
  • (B) Under the ifใต้ if
    Correct! It counts only the rounds where the name is on the other list.ถูกต้อง! นับเฉพาะรอบที่พบชื่อในอีก list
  • (C) Before the forก่อน for
    Wrong: no name has been checked at that point.ผิด: ตรงนั้นยังไม่ได้ตรวจชื่อ
  • (D) After the printหลัง print
    Wrong: the value has already been shown by then.ผิด: ค่าที่แสดงไปแล้วจะไม่เปลี่ยน

ข้อ 10

10 / 10

How can the program show the names of the students in both clubs?
both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)
จะแสดงชื่อคนที่สมัครทั้ง 2 ชมรมได้อย่างไร?
both = 0
for student in photo_club:
    if student in cooking_club:
        both += 1
print(both)

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) print(student) under the ifprint(student) ใต้ if

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) print(both) under the ifprint(both) ใต้ if
    Wrong: both holds a count, not a name.ผิด: both เก็บจำนวน ไม่ใช่ชื่อ
  • (B) print(photo_club) outside the loopprint(photo_club) นอก loop
    Wrong: it shows every name of the photography club.ผิด: จะแสดงทุกชื่อของชมรมถ่ายภาพ
  • (C) print(cooking_club) under the forprint(cooking_club) ใต้ for
    Wrong: it shows the whole list in every round.ผิด: จะแสดงทั้ง list ทุกรอบ
  • (D) print(student) under the ifprint(student) ใต้ if
    Correct! That line runs only for a matching name.ถูกต้อง! บรรทัดนี้ทำงานเฉพาะชื่อที่ตรงกัน