Example 2 — Triangle Perimeter & Area ตัวอย่างที่ 2 — เส้นรอบรูปและพื้นที่สามเหลี่ยม
A worked problem: read the three side lengths of a triangle, check that they really form a triangle, then report the perimeter and the area with Heron's formula. โจทย์ตัวอย่าง: รับความยาวด้านทั้งสามของสามเหลี่ยม ตรวจสอบว่าประกอบกันเป็นสามเหลี่ยมได้จริง แล้วรายงานเส้นรอบรูปและพื้นที่ด้วยสูตรของเฮรอน
Read 3 numbers — the side lengths a, b, c of a triangle — and store them in a list sides. Then use three user-defined functions:
รับ ตัวเลข 3 ค่า คือความยาวด้าน a, b, c ของสามเหลี่ยม แล้วเก็บไว้ในลิสต์ชื่อ sides จากนั้นใช้ฟังก์ชันที่นิยามเอง 3 ตัว:
- is_valid_triangle(sides) — True only when every side is shorter than the sum of the other two
- calculate_perimeter(sides) — the perimeter (the total of all sides)
- calculate_area(sides) — the area, using Heron's formula
- is_valid_triangle(sides) — ได้ True เฉพาะเมื่อทุกด้านสั้นกว่าผลบวกของอีกสองด้าน
- calculate_perimeter(sides) — เส้นรอบรูป (ผลรวมของทุกด้าน)
- calculate_area(sides) — พื้นที่ ด้วยสูตรของเฮรอน
Heron's formula uses the half-perimeter \(s = \dfrac{a + b + c}{2}\): \(\text{Area} = \sqrt{s\,(s-a)\,(s-b)\,(s-c)}\).
สูตรของเฮรอนใช้ครึ่งหนึ่งของเส้นรอบรูป \(s = \dfrac{a + b + c}{2}\): \(\text{Area} = \sqrt{s\,(s-a)\,(s-b)\,(s-c)}\)
When the sides do not form a triangle, print "the triangle sides are not valid" and stop.
ถ้าด้านที่รับมาไม่เป็นสามเหลี่ยม ให้พิมพ์ "the triangle sides are not valid" แล้วจบ
Ideas used: a counting loop (for / while), a list, user-defined functions (def / return), modulo indexing ((i + 1) % N) to wrap around the list, an accumulator (Total), a running product (A), import math with math.sqrt, and one function calling another.
แนวคิดที่ใช้: loop นับรอบ (for / while), list, ฟังก์ชันที่นิยามเอง (def / return), การวนดัชนีแบบ modulo ((i + 1) % N) เพื่อวนกลับต้นลิสต์, ตัวสะสมค่า (Total), ตัวคูณสะสม (A), import math กับ math.sqrt และการเรียกฟังก์ชันหนึ่งจากอีกฟังก์ชันหนึ่ง
The program starts by creating an empty list sides to hold the lengths, a list side_name that holds ['a', 'b', 'c'] for the prompts, and i = 0 as the data counter. Next it checks whether i < 3; if it is, an input asks the user for the next side value, the number is added to sides with append, i is increased by 1, and control goes back to test i again. This repeats until i < 3 is false, so it leaves the loop. Then it calls is_valid_triangle and keeps the answer in is_valid_data. It checks whether is_valid_data is True; if it is, it finds the perimeter and the area with the helper functions calculate_perimeter and calculate_area in turn and shows both with print; if it is not, it prints "the triangle sides are not valid". Either way the program then ends.
โปรแกรมนี้เริ่มต้นด้วยสร้างลิสต์ว่างชื่อ sides ไว้เก็บความยาวด้าน, ลิสต์ side_name ที่เก็บ ['a', 'b', 'c'] ไว้ใช้ในข้อความถาม และ i = 0 เป็นตัวนับจำนวนข้อมูล ถัดไปตรวจสอบเงื่อนไขว่า i < 3 หรือไม่ ถ้าน้อยกว่าสร้าง input ให้ผู้ใช้กรอกความยาวด้านถัดไป แล้วเพิ่มตัวเลขนั้นเข้าไปที่ sides ด้วยคำสั่ง append จากนั้นเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < 3 เป็นเท็จ จึงออกจาก loop แล้วเรียก is_valid_triangle เก็บคำตอบไว้ที่ is_valid_data ถัดไปตรวจสอบว่า is_valid_data เป็น True หรือไม่ ถ้าใช่ให้หาเส้นรอบรูปและพื้นที่ด้วย function ย่อยชื่อ calculate_perimeter และ calculate_area ตามลำดับ แล้วแสดงทั้งสองค่าด้วยคำสั่ง print ถ้าไม่ใช่ให้พิมพ์ข้อความ "the triangle sides are not valid" จากนั้นจบการทำงานของโปรแกรม
Enter side a: 3 Enter side b: 4 Enter side c: 5 Perimeter: 12.0 Area: 6.00
This function starts by setting N = len(sides) as the number of sides and i = 0 as the counter. Next it checks whether i < len(sides); if it is, it builds three indices i1 = i, i2 = (i + 1) % N and i3 = (i + 2) % N so the two "other" sides wrap around the list. Then it checks whether sides[i2] + sides[i3] > sides[i1]; if yes it increases i by 1 and goes back to test i again, if no it sends False back with return straight away. When the loop has checked every side without failing, that is, i < len(sides) is false, it leaves the loop and returns True.
ฟังก์ชันนี้เริ่มต้นด้วยกำหนดค่า N = len(sides) เป็นจำนวนด้าน และ i = 0 เป็นตัวนับ ถัดไปตรวจสอบเงื่อนไขว่า i < len(sides) หรือไม่ ถ้าน้อยกว่าให้สร้างดัชนีสามตัว i1 = i, i2 = (i + 1) % N และ i3 = (i + 2) % N เพื่อให้อีกสองด้านวนกลับมาต้นลิสต์ได้ แล้วตรวจสอบว่า sides[i2] + sides[i3] > sides[i1] หรือไม่ ถ้าใช่ให้เพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ถ้าไม่ใช่ให้ส่งค่า False กลับด้วยคำสั่ง return ทันที เมื่อตรวจครบทุกด้านโดยไม่มีด้านใดล้มเหลว นั่นคือ i < len(sides) เป็นเท็จ จึงออกจาก loop แล้วส่งค่า True กลับ
Run just this function on two fixed lists — a real triangle [3, 4, 5] and a bad one [1, 2, 5]:
ลองรันเฉพาะฟังก์ชันนี้กับลิสต์ค่าคงที่ 2 ชุด — สามเหลี่ยมจริง [3, 4, 5] และชุดที่ไม่ใช่ [1, 2, 5]:
True False
This function starts by setting Total = 0 as an accumulator and i = 0 as the index. Next it checks whether i < len(sides); if it is, it takes side = sides[i], adds it into Total with Total += side, then increases i by 1 and goes back to test i again. This repeats until i < len(sides) is false, so it leaves the loop and sends Total back with return.
ฟังก์ชันนี้เริ่มต้นด้วยกำหนดค่า Total = 0 เป็นตัวสะสมค่า และ i = 0 เป็นดัชนี ถัดไปตรวจสอบเงื่อนไขว่า i < len(sides) หรือไม่ ถ้าน้อยกว่าให้ดึง side = sides[i] บวกเข้าไปที่ Total ด้วย Total += side แล้วเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < len(sides) เป็นเท็จ จึงออกจาก loop แล้วส่งค่า Total กลับด้วยคำสั่ง return
12
Note: the source diagram has two slips in this box — it multiplies before giving A a starting value, and it writes side[i]. This page fixes both: it sets A = S first, then multiplies by (S - side) for each side.
หมายเหตุ: แผนภาพต้นฉบับมีจุดพลาด 2 จุดในกล่องนี้ — มันคูณก่อนกำหนดค่าเริ่มต้นให้ A และเขียนว่า side[i] หน้านี้แก้ทั้งสองจุด: กำหนด A = S ก่อน แล้วจึงคูณด้วย (S - side) ทีละด้าน
This function starts by calling calculate_perimeter(sides) and keeping the result in p, then computes the half-perimeter S = p / 2, sets the running product A = S, and i = 0 as the index. Next it checks whether i < len(sides); if it is, it takes side = sides[i], updates A = A * (S - side), then increases i by 1 and goes back to test i again. This repeats until i < len(sides) is false, so it leaves the loop, computes Area = math.sqrt(A), and sends Area back with return.
ฟังก์ชันนี้เริ่มต้นด้วยเรียก calculate_perimeter(sides) เก็บผลไว้ที่ p แล้วคำนวณครึ่งหนึ่งของเส้นรอบรูป S = p / 2 กำหนดตัวคูณสะสม A = S และ i = 0 เป็นดัชนี ถัดไปตรวจสอบเงื่อนไขว่า i < len(sides) หรือไม่ ถ้าน้อยกว่าให้ดึง side = sides[i] ปรับ A = A * (S - side) แล้วเพิ่มค่า i ขึ้น 1 แล้วกลับไปตรวจสอบค่า i อีกครั้ง ทำแบบนี้จน i < len(sides) เป็นเท็จ จึงออกจาก loop คำนวณ Area = math.sqrt(A) แล้วส่งค่า Area กลับด้วยคำสั่ง return
6.0
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, one function calling another) (def/return, ฟังก์ชันเรียกฟังก์ชัน) -
math — math.sqrt
(square root in
calculate_area) (รากที่สองในcalculate_area) -
Binary arithmetic operations
(the
%operator for wrap-around indexing) (ตัวดำเนินการ%สำหรับวนดัชนีกลับต้นลิสต์) - Heron's formula (the area formula used in card 1 and card 5) (สูตรพื้นที่ที่ใช้ในการ์ด 1 และการ์ด 5)
- Flowchart display and symbols follow 01 Basic → Flowchart Basic. รูปแบบการแสดง flowchart และสัญลักษณ์อ้างอิงจาก 01 Basic → Flowchart Basic