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

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

Worked problems, debugging problems and better solutions to earlier problems that bring the earlier lessons together โจทย์ตัวอย่าง โจทย์ debugging และวิธีที่ดีกว่าของโจทย์ข้อก่อนหน้า ที่นำเนื้อหาบทก่อนหน้ามารวมกัน

Overview ภาพรวม

The earlier lessons teach programming one concept at a time, but a real program has to combine those pieces to do a job or solve a problem. This section presents short example programs with a flowchart explanation and Python code that you can test and change, so that you can put what you learned in the earlier Python lessons to use: for example loops (for and while), variables, functions (def) and decisions (if-else).

บทเรียนก่อนหน้านี้สอนแนวคิดการเขียนโปรแกรมทีละส่วน แต่ในโปรแกรมจริงต้องนำแต่ละส่วนในบทก่อนหน้านี้มารวมกันเพื่อให้ได้โปรแกรมที่ทำงานบางอย่างได้ หรือแก้ไขปัญหา ซึ่งในหัวข้อนี้จะนำเสนอตัวอย่างโปรแกรมสั้นๆ พร้อมคำอธิบายแบบแผนภาพ และโค้ด Python ที่ทดสอบและแก้ไขได้ เพื่อให้ผู้อ่านได้นำความรู้จากการเขียนโปรแกรมในหัวข้อ Python ก่อนหน้านี้มาใช้งาน เช่น การเขียนโปรแกรมแบบลูป (for และ while loop) การใช้งานตัวแปร ฟังก์ชันย่อย (def) และการเขียนโปรแกรมแบบตัดสินใจ (if-else)

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

Every page has a problem description, a flowchart with its explanation, and runnable Python (simulated with JavaScript, as in the lessons). Some examples have an input box where you type values, just like sending data through input() in Python.

ทุกหน้ามี คำอธิบายโจทย์ flowchart พร้อมคำอธิบาย และ โค้ด Python ที่รันได้ (จำลองด้วย JavaScript เหมือนในบทเรียน) บางตัวอย่างมีช่อง input ให้กรอกเหมือนการส่งข้อมูลผ่าน input() ในภาษา Python

1. Worked example
e.g. Example 1 — Exam Score Analysis

  • Read the problem, then try to sketch the steps yourself before looking further.
  • Compare your sketch with the flowchart explanation and the diagram.
  • Run the code, then change the input values and predict the output before you run it again.

1. ตัวอย่างพร้อมคำอธิบาย
เช่น ตัวอย่างที่ 1 — วิเคราะห์คะแนนสอบ

  • อ่านโจทย์ แล้วลองร่างขั้นตอนด้วยตัวเองก่อนอ่านต่อ
  • เทียบกับคำอธิบาย flowchart และแผนภาพ
  • รันโค้ด แล้วเปลี่ยนค่าที่ป้อนและทายผลลัพธ์ก่อนกดรันอีกครั้ง

2. Debugging problem (the title ends with “(debugging)”)
e.g. Example 4 — Find the Biggest Sensor Jump

  • The program looks finished but hides exactly one bug. Check the code against the flowchart.
  • Run every row of the test list (normal, boundary and error cases) and compare the expected output with the output of the buggy code.
  • Work through Your tasks: look for what the failing inputs have in common, add print inside the loop and fill in the trace table.
  • Find and fix the bug yourself, then open the Solution to compare with the fixed code.

2. โจทย์ debugging (ชื่อหน้าลงท้ายด้วย “(debugging)”)
เช่น ตัวอย่างที่ 4 — หาจุดที่ค่าเซนเซอร์เปลี่ยนมากที่สุด

  • โปรแกรมดูเหมือนเขียนเสร็จแล้ว แต่มี bug ซ่อนอยู่ 1 จุด ให้ตรวจโค้ดเทียบกับ flowchart
  • รันทุกแถวในรายการทดสอบ (normal, boundary และ error) แล้วเทียบผลที่ควรได้กับผลจากโค้ดที่มี bug
  • ทำตามหัวข้อภารกิจ: หาว่า input ที่ให้ผลผิดมีอะไรเหมือนกัน เพิ่ม print ใน loop แล้วกรอกลง trace table
  • หาและแก้ bug เอง แล้วจึงเปิดเฉลยเพื่อเทียบกับโค้ดที่แก้แล้ว

3. Better solution to an earlier problem (the title ends with “with One Loop” or similar)
e.g. Example 7 — Check a Palindrome with One Loop, which follows Example 6

  • Do the earlier example first; each of these pages names it near the start of its content.
  • Run the code and check it against the Results of the program table.
  • Read Why this program is better than Example …, which compares the two programs, for example by the number of loops, the extra variables and the number of rounds.

3. วิธีที่ดีกว่าของโจทย์ข้อก่อนหน้า (ชื่อหน้าลงท้ายด้วย “ด้วย loop เดียว” หรือคล้ายกัน)
เช่น ตัวอย่างที่ 7 — ตรวจคำที่อ่านกลับหลังได้เหมือนเดิมด้วย loop เดียว ซึ่งทำต่อจากตัวอย่างที่ 6

  • ทำตัวอย่างข้อก่อนหน้านี้ ซึ่งระบุไว้ในช่วงต้นของเนื้อหานั้นๆ
  • รันโค้ดแล้วเทียบกับตารางผลของโปรแกรม
  • อ่านหัวข้อโปรแกรมนี้ดีกว่าตัวอย่างที่ … อย่างไร ซึ่งเปรียบเทียบสองโปรแกรม เช่น จำนวน loop ตัวแปรเพิ่ม และจำนวนรอบ

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

  • Learners who have finished the lessons on control structures and want to practise putting them together.
  • Anyone who can follow a flowchart but is unsure how to turn it into code.
  • Learners who want a check before the data files lesson.
  • Learners who want to practise testing and debugging; study Testing and Debugging first, as it explains the kinds of error, the kinds of test case and the bug-hunting tools the debugging problems use.
  • ผู้เรียนที่จบบท โครงสร้างควบคุม แล้วและอยากฝึกประกอบแนวคิดเข้าด้วยกัน
  • ผู้ที่อ่าน flowchart ได้แต่ยังไม่แน่ใจว่าจะเขียนเป็นโค้ดอย่างไร
  • ผู้ที่อยากทบทวนก่อนเรียนบท ไฟล์ข้อมูล
  • ผู้ที่อยากฝึกทดสอบและหา bug ควรเรียนบท Testing and Debugging ก่อน เพราะบทนั้นอธิบายชนิดของ error ชนิดของ test case และเครื่องมือหา bug ที่โจทย์ debugging ใช้

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

  • Counting loops, and while loops with a stop condition or while True with break
  • Lists, index assignment and negative indexes; counting with a dictionary
  • User-defined functions with def and return
  • Accumulators and running maximums
  • Conditions joined with and / or, and validating input before calculating
  • Turning typed text into numbers with split and int; working with letters using isalpha, upper and lower
  • The math module and a circular buffer built with %
  • Testing: normal, boundary and error test cases, and comparing expected with actual output
  • Debugging: tracing a loop with print and a trace table
  • Comparing two working programs, for example by their number of loops and rounds
  • ลูปนับรอบ และลูป while ที่มีเงื่อนไขหยุดหรือ while True กับ break
  • list การเขียนทับค่าด้วยดัชนี และดัชนีติดลบ รวมถึงการนับด้วย dictionary
  • ฟังก์ชันที่นิยามเองด้วย def และ return
  • ตัวสะสมค่าและค่ามากสุดระหว่างวน
  • เงื่อนไขที่เชื่อมด้วย and / or และการตรวจสอบค่าก่อนคำนวณ
  • การแปลงข้อความที่ป้อนเป็นตัวเลขด้วย split และ int และการจัดการตัวอักษรด้วย isalpha, upper และ lower
  • โมดูล math และ circular buffer ด้วย %
  • การทดสอบ: test case แบบ normal, boundary และ error และการเทียบผลที่ควรได้กับผลจริง
  • การหา bug: trace loop ด้วย print และ trace table
  • การเปรียบเทียบโปรแกรมสองแบบที่ทำงานได้ทั้งคู่ เช่น จากจำนวน loop และจำนวนรอบ

By the end of this section, we hope you will be able to break a small problem into steps, draw the flowchart, turn it into working Python, test it with well-chosen cases, find and fix a bug, and make a program simpler.

เมื่อจบหมวดนี้ หวังว่าผู้อ่านจะแตกปัญหาเล็กๆ เป็นขั้นตอน วาด flowchart แปลงเป็นโค้ด Python ที่ทำงานได้ ทดสอบด้วย test case ที่เลือกมาอย่างดี หาและแก้ bug และปรับโปรแกรมให้เรียบง่ายขึ้นได้

example