Example programs ตัวอย่างโปรแกรม

Worked problems that bring the earlier lessons together โจทย์ตัวอย่างที่นำเนื้อหาบทก่อนหน้ามารวมกัน

Overview ภาพรวม

A lesson teaches one idea at a time; a real program needs several ideas working together. These three worked problems take a short, realistic task — analysing exam marks, measuring a triangle, smoothing a stream of readings — and solve it step by step, so you can see how variables, loops, lists, functions and decisions fit into one program.

บทเรียนสอนทีละแนวคิด แต่โปรแกรมจริงต้องใช้หลายแนวคิดทำงานร่วมกัน สามโจทย์ตัวอย่างนี้หยิบงานสั้นๆ ที่ใกล้เคียงของจริง ได้แก่ วิเคราะห์คะแนนสอบ วัดสามเหลี่ยม และทำให้ค่าที่อ่านได้เรียบขึ้น แล้วแก้ทีละขั้น เพื่อให้เห็นว่าตัวแปร ลูป list ฟังก์ชัน และการตัดสินใจประกอบกันเป็นโปรแกรมเดียวได้อย่างไร

How to use the lessons วิธีใช้บทเรียน

Each example page has four parts: a problem description, a flowchart explanation, the flowchart diagram itself, and runnable Python (simulated with JavaScript, as in the lessons). A short multiple-choice exercise follows at the end.

แต่ละหน้าตัวอย่างมี 4 ส่วน: คำอธิบายโจทย์ คำอธิบาย flowchart แผนภาพ flowchart และ โค้ด Python ที่รันได้ (จำลองด้วย JavaScript เหมือนในบทเรียน) ปิดท้ายด้วยแบบฝึกหัดแบบตัวเลือกสั้นๆ

  1. Read the problem, then close the page and try to sketch the steps yourself.
  2. Compare your sketch with the flowchart explanation and the diagram.
  3. Run the code, then change the input values and predict the output before you run it again.
  4. Finish with the exercise to check that you can explain each part.
  1. อ่านโจทย์ แล้วลองร่างขั้นตอนด้วยตัวเองก่อนดูเฉลย
  2. เทียบกับคำอธิบาย flowchart และแผนภาพ
  3. รันโค้ด แล้วเปลี่ยนค่าที่ป้อนและทายผลลัพธ์ก่อนกดรันอีกครั้ง
  4. ปิดท้ายด้วยแบบฝึกหัดเพื่อตรวจว่าอธิบายแต่ละส่วนได้

Who it is for เหมาะกับใคร

Learners who have finished the lessons on control structures and want to practise putting them together, and anyone who can follow a flowchart but is unsure how to turn it into code. It is also a good check before the data files lesson.

ผู้เรียนที่จบบท โครงสร้างควบคุม แล้วและอยากฝึกประกอบแนวคิดเข้าด้วยกัน และผู้ที่อ่าน flowchart ได้แต่ยังไม่แน่ใจว่าจะเขียนเป็นโค้ดอย่างไร ใช้ทบทวนก่อนเรียนบท ไฟล์ข้อมูล ก็ได้

Learning order ลำดับการเรียนรู้

  1. Exam Score Analysis — the gentlest: a loop, a list and three small functions
  2. Triangle Perimeter & Area — adds validation, while, % indexing and math
  3. Moving Average — the hardest: a circular window and a stop condition
  1. วิเคราะห์คะแนนสอบ — ง่ายที่สุด: ลูป list และฟังก์ชันเล็กๆ สามตัว
  2. เส้นรอบรูปและพื้นที่สามเหลี่ยม — เพิ่มการตรวจสอบค่า while การวนดัชนีด้วย % และ math
  3. ค่าเฉลี่ยเคลื่อนที่ — ยากที่สุด: หน้าต่างแบบวงกลมและเงื่อนไขหยุด

Take them in this order: each example reuses ideas from the one before it.

เรียนตามลำดับนี้ เพราะแต่ละตัวอย่างใช้แนวคิดจากตัวอย่างก่อนหน้า

What this section covers เนื้อหาในหมวดนี้

Together the examples practise counting loops and while loops with a stop condition, lists and index assignment, user-defined functions with def and return, accumulators and running maximums, compound conditions with and, validating input before calculating, the math module with Heron’s formula, and a circular buffer built with %.

ตัวอย่างทั้งสามฝึก ลูปนับรอบและลูป while ที่มีเงื่อนไขหยุด, list และการเขียนทับค่าด้วยดัชนี, ฟังก์ชันที่นิยามเองด้วย def และ return, ตัวสะสมค่าและค่ามากสุดระหว่างวน, เงื่อนไขแบบรวมด้วย and, การตรวจสอบค่าก่อนคำนวณ, โมดูล math กับสูตรของ Heron และ circular buffer ด้วย %

When you finish you can break a small problem into steps, draw the flowchart, and turn it into working Python with functions, loops and decisions.

เมื่อจบหมวดนี้ คุณจะแตกปัญหาเล็กๆ เป็นขั้นตอน วาด flowchart และแปลงเป็นโค้ด Python ที่ทำงานได้ ด้วยฟังก์ชัน ลูป และการตัดสินใจ

example

โจทย์ตัวอย่างพร้อมคำอธิบาย flowchart แผนภาพ และโค้ด Python ที่รันได้