Example 1 — Exam Score Analysis ตัวอย่างที่ 1 — วิเคราะห์คะแนนสอบ
A worked problem: read ten exam marks, then report the class average, how many students passed, and the highest mark, and finally judge the class. โจทย์ตัวอย่าง: รับคะแนนสอบ 10 คน แล้วรายงานค่าเฉลี่ยของชั้น จำนวนคนที่สอบผ่าน และคะแนนสูงสุด สุดท้ายสรุปผลของชั้นเรียน
A class has 10 students. Read each student's exam mark (a whole number from 0 to 100) and store the marks in a list. Then produce three results using three user-defined functions:
ชั้นเรียนมีนักเรียน 10 คน ให้รับคะแนนสอบของแต่ละคน (จำนวนเต็ม 0 ถึง 100) แล้วเก็บคะแนนไว้ในลิสต์ จากนั้นสร้างผลลัพธ์ 3 อย่างด้วยฟังก์ชันที่นิยามเอง 3 ตัว:
- find_average(marks) — the class average (total ÷ N)
- count_passes(marks) — how many marks are ≥ 50 (a pass)
- find_highest(marks) — the single highest mark
- find_average(marks) — ค่าเฉลี่ยของชั้น (ผลรวม ÷ N)
- count_passes(marks) — จำนวนคะแนนที่ ≥ 50 (ถือว่าผ่าน)
- find_highest(marks) — คะแนนสูงสุดเพียงค่าเดียว
Finally, print "Good class performance" when the average is >= 70 and the number of passes is >= 8; otherwise print "Needs improvement".
สุดท้าย ให้พิมพ์ "Good class performance" เมื่อค่าเฉลี่ย >= 70 และ จำนวนคนที่ผ่าน >= 8; ถ้าไม่เข้าเงื่อนไข ให้พิมพ์ "Needs improvement"
Ideas used: a counting loop (for / while), a list, user-defined functions (def / return), an accumulator (Total), a running maximum (Highest), and a compound if condition with and.
แนวคิดที่ใช้: loop นับรอบ (for / while), list, ฟังก์ชันที่นิยามเอง (def / return), ตัวสะสมค่า (Total), ตัวเก็บค่ามากสุดระหว่างวน (Highest) และเงื่อนไข if แบบรวมด้วย and
The program starts by setting i = 0 as the data counter (after 10 values it moves on to the next step) and creating a list marks to hold the scores. Next it checks whether i < 10; if it is, an input asks the user for a number 0–100, then the entered number is added to marks with append, i is increased by 1, and control goes back to test i again. This repeats until i < 10 is false, so it leaves the loop. Then it finds the average score, the number of passing scores, and the highest score using the sub-functions find_average, count_passes, and Find_highest in turn. When every value is computed it shows the results with print. After that it checks whether the average is >= 70 and the number of passes is >= 8; if so it uses print for the message "Good class performance", otherwise it prints "Needs improvement", and the program ends.
โปรแกรมนี้เริ่มต้นด้วยกำหนดค่า i = 0 เป็นตัวนับจำนวนข้อมูล (เมื่อรับค่าครบ 10 ครั้งแล้วทำงานขั้นตอนถัดไป) และสร้างตัวแปรสำหรับเก็บคะแนนมีชื่อว่า marks ถัดไปตรวจสอบเงื่อนไขว่า i < 10 หรือไม่ ถ้าน้อยกว่าสร้าง input ให้ผู้ใช้กรอกตัวเลข 0–100 แล้วเพิ่มตัวเลขที่ผู้ใช้ป้อนเข้ามาไปเก็บที่ตัวแปร marks ด้วยคำสั่ง append ถัดไปเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < 10 เป็นเท็จ จึงออกจาก loop แล้วทำการหาค่าคะแนนเฉลี่ย จำนวนคะแนนที่ผ่าน และคะแนนสูงสุดด้วย function ย่อย ชื่อ find_average, count_passes และ Find_highest ตามลำดับ เมื่อคำนวณทุกค่าเสร็จแล้วแสดงผลลัพธ์ด้วยคำสั่ง print หลังจากนั้นตรวจสอบค่าเฉลี่ยว่า มากกว่าเท่ากับ 70 และจำนวนที่ผ่านมากกว่าเท่ากับ 8 หรือไม่ ถ้าใช่ให้ print ข้อความ "Good class performance" แต่ถ้าไม่ให้ print ข้อความ "Needs improvement" แล้วจบการทำงานของโปรแกรม
Enter mark 1 (0-100): 82 Enter mark 2 (0-100): 47 Enter mark 3 (0-100): 90 Enter mark 4 (0-100): 66 Enter mark 5 (0-100): 51 Enter mark 6 (0-100): 73 Enter mark 7 (0-100): 88 Enter mark 8 (0-100): 40 Enter mark 9 (0-100): 95 Enter mark 10 (0-100): 78 Average: 71.00 Passes: 8 Highest: 95 Good class performance
This function starts by setting Total = 0 as an accumulator and N = len(marks) as the number of scores, and i = 0 as the counter. Next it checks whether i < len(marks); if it is, it takes mark = marks[i], adds it into Total, then increases i by 1 and goes back to test i again. This repeats until i < len(marks) is false, so it leaves the loop and computes Average_value = Total / N, then sends that value back with return.
ฟังก์ชันนี้เริ่มต้นด้วยกำหนดค่า Total = 0 เป็นตัวสะสมค่า และ N = len(marks) เป็นจำนวนคะแนน กับ i = 0 เป็นตัวนับ ถัดไปตรวจสอบเงื่อนไขว่า i < len(marks) หรือไม่ ถ้าน้อยกว่าให้ดึง mark = marks[i] บวกเข้าไปที่ Total แล้วเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < len(marks) เป็นเท็จ จึงออกจาก loop แล้วคำนวณ Average_value = Total / N แล้วส่งค่านี้กลับด้วยคำสั่ง return
Run just this function on the fixed list:
ลองรันเฉพาะฟังก์ชันนี้กับลิสต์ค่าคงที่:
71.0
This function starts by setting count_passes = 0 as a counter and i = 0 as the index. Next it checks whether i < len(marks); if it is, it takes mark = marks[i] and checks whether mark >= 50; if yes it increases count_passes by 1, if no it does nothing. Then it increases i by 1 and goes back to test i again. This repeats until i < len(marks) is false, so it leaves the loop and sends count_passes back with return.
ฟังก์ชันนี้เริ่มต้นด้วยกำหนดค่า count_passes = 0 เป็นตัวนับ และ i = 0 เป็นดัชนี ถัดไปตรวจสอบเงื่อนไขว่า i < len(marks) หรือไม่ ถ้าน้อยกว่าให้ดึง mark = marks[i] แล้วตรวจสอบว่า mark >= 50 หรือไม่ ถ้าใช่ให้เพิ่มค่า count_passes ขึ้น 1 ถ้าไม่ใช่ไม่ต้องทำอะไร จากนั้นเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < len(marks) เป็นเท็จ จึงออกจาก loop แล้วส่งค่า count_passes กลับด้วยคำสั่ง return
8
This function starts by setting Highest = -inf (negative infinity) as the running maximum, so the very first mark is always bigger, and i = 0 as the index. Next it checks whether i < len(marks); if it is, it takes mark = marks[i] and checks whether mark > Highest; if yes it updates Highest = mark, if no it keeps the old value. Then it increases i by 1 and goes back to test i again. This repeats until i < len(marks) is false, so it leaves the loop and sends Highest_value back with return.
ฟังก์ชันนี้เริ่มต้นด้วยกำหนดค่า Highest = -inf (ลบอนันต์) เป็นตัวเก็บค่ามากสุด เพื่อให้คะแนนตัวแรกมากกว่าเสมอ และ i = 0 เป็นดัชนี ถัดไปตรวจสอบเงื่อนไขว่า i < len(marks) หรือไม่ ถ้าน้อยกว่าให้ดึง mark = marks[i] แล้วตรวจสอบว่า mark > Highest หรือไม่ ถ้าใช่ให้ปรับ Highest = mark ถ้าไม่ใช่คงค่าเดิม จากนั้นเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < len(marks) เป็นเท็จ จึงออกจาก loop แล้วส่งค่า Highest_value กลับด้วยคำสั่ง return
Note: the source diagram labels the end box "Return count_passes" — that is a slip. This function returns the highest mark, so the box below reads return Highest_value.
หมายเหตุ: แผนภาพต้นฉบับเขียนกล่องท้ายว่า "Return count_passes" ซึ่งเป็นการพิมพ์ผิด ฟังก์ชันนี้ส่งค่าคะแนนสูงสุดกลับ กล่องด้านล่างจึงเป็น return Highest_value
95
Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab.
หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab
References อ้างอิง
-
Python Tutorial — Defining Functions
(
def/return) (def/return) - Built-in Functions — range / len (walking a list by index) (วนลิสต์ด้วยดัชนี)
- Flowchart display and symbols follow 01 Basic → Flowchart Basic (card 12). รูปแบบการแสดง flowchart และสัญลักษณ์อ้างอิงจาก 01 Basic → Flowchart Basic (card 12)