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. โจทย์ตัวอย่าง: รับความยาวด้านทั้งสามของสามเหลี่ยม ตรวจสอบว่าประกอบกันเป็นสามเหลี่ยมได้จริง แล้วรายงานเส้นรอบรูปและพื้นที่ด้วยสูตรของเฮรอน

Triangle Perimeter & Area เส้นรอบรูปและพื้นที่สามเหลี่ยม
1 The problem โจทย์

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 และการเรียกฟังก์ชันหนึ่งจากอีกฟังก์ชันหนึ่ง

2 Main program — flowchart explanation โปรแกรมหลัก — คำอธิบาย flowchart

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" จากนั้นจบการทำงานของโปรแกรม

Start sides = [] side_name = ['a', 'b', 'c'] i = 0 loop i<3 No Yes Input: side value (a, b, c) sides.append(number) i++ is_valid_data = is_valid_triangle(sides) is_valid_data == True ? Yes No Output: “the triangle sides are not valid” perimeter = calculate_perimeter(sides) area = calculate_area(sides) Output: perimeter, area Stop
The runnable code below reads the 3 sides with input(), exactly like the flowchart. Edit the three numbers after the 1: … 3: labels in the [input] box, then press Run. โค้ดที่รันได้ด้านล่างรับด้านทั้ง 3 ด้วย input() ตรงตาม flowchart แก้ตัวเลขทั้ง 3 ค่าหลังเลข 1: … 3: ในกล่อง [input] แล้วกด Run
[main]
[input]
[output]
Enter side a: 3
Enter side b: 4
Enter side c: 5
Perimeter: 12.0
Area: 6.00
3 is_valid_triangle(sides)

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 กลับ

is_valid_triangle(sides) N = len(sides) i = 0 loop i<len(sides) No Yes i1 = i i2 = (i + 1) % N i3 = (i + 2) % N sides[i2] + sides[i3] > sides[i1] ? Yes i++ No Return False Return True
The % (modulo) keeps the two "other" side indices inside the list: for i = 2 they wrap to 0 and 1. % (มอดุโล) ทำให้ดัชนีของอีกสองด้านอยู่ในลิสต์เสมอ: เมื่อ i = 2 จะวนกลับเป็น 0 และ 1

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]:

[demo]
[output]
True
False
4 calculate_perimeter(sides)

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

calculate_perimeter(sides) Total = 0 i = 0 loop i<len(sides) No Yes side = sides[i] Total += side i++ Return Total
Total is an accumulator: it starts at 0 and each side length is added in. Total เป็น ตัวสะสมค่า: เริ่มที่ 0 แล้วบวกความยาวด้านทีละค่าเข้าไป
[demo]
[output]
12
5 calculate_area(sides)

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

calculate_area(sides) p = calculate_perimeter(sides) S = p / 2 A = S i = 0 loop i<len(sides) No Yes side = sides[i] A = A * (S - side) i++ Area = math.sqrt(A) Return Area
A is a running product: it starts at S, then is multiplied by (S - side) once per side, giving \(s\,(s-a)(s-b)(s-c)\). A เป็น ตัวคูณสะสม: เริ่มที่ S แล้วคูณด้วย (S - side) หนึ่งครั้งต่อหนึ่งด้าน ได้เป็น \(s\,(s-a)(s-b)(s-c)\)
[demo]
[output]
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

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

How many side lengths does the main program read?โปรแกรมหลักรับความยาวด้านทั้งหมดกี่ค่า?

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

เฉลย / Answer: (A) 3 values (loop i < 3)3 ค่า (loop i < 3)

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

  • (A) 3 values (loop i < 3)3 ค่า (loop i < 3)
    Correct! The loop runs while i < 3, so it reads 3 values: a, b, c.ถูกต้อง! loop วนขณะ i < 3 จึงรับ 3 ค่า คือ a, b, c
  • (B) 2 values2 ค่า
    Wrong: a triangle has 3 sides and the condition is i < 3.ผิด: สามเหลี่ยมมี 3 ด้าน และเงื่อนไขคือ i < 3
  • (C) 4 values4 ค่า
    Wrong: the condition is i < 3, not i < 4.ผิด: เงื่อนไขคือ i < 3 ไม่ใช่ i < 4
  • (D) Unlimited, until the user stopsไม่จำกัด จนกว่าผู้ใช้จะหยุด
    Wrong: the counting loop with i has a clear bound of 3.ผิด: loop นับรอบด้วย i มีขอบเขตชัดเจนที่ 3

ข้อ 2

2 / 10

What does calculate_perimeter(sides) return?calculate_perimeter(sides) ส่งค่าอะไรกลับ?

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

เฉลย / Answer: (B) The total of all the side lengthsผลรวมของความยาวทุกด้าน

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

  • (A) The area of the triangleพื้นที่ของสามเหลี่ยม
    Wrong: the area is calculate_area's job.ผิด: พื้นที่เป็นงานของ calculate_area
  • (B) The total of all the side lengthsผลรวมของความยาวทุกด้าน
    Correct! The loop adds every side into Total, then returns Total.ถูกต้อง! ลูปบวกทุกด้านเข้า Total แล้ว return Total
  • (C) Half of the perimeter (S)ครึ่งหนึ่งของเส้นรอบรูป (S)
    Wrong: the halving happens in calculate_area, not here.ผิด: การหารครึ่งเกิดใน calculate_area ไม่ใช่ที่นี่
  • (D) The longest sideด้านที่ยาวที่สุด
    Wrong: this function does not compare sides.ผิด: ฟังก์ชันนี้ไม่ได้เปรียบเทียบด้าน

ข้อ 3

3 / 10

In is_valid_triangle, what must hold for every side to have a triangle?ใน is_valid_triangle ทุกด้านต้องเป็นจริงตามข้อใดจึงจะเป็นสามเหลี่ยม?

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

เฉลย / Answer: (C) The side is shorter than the sum of the other twoด้านนั้นสั้นกว่าผลบวกของอีกสองด้าน

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

  • (A) The side equals the sum of the other twoด้านนั้นเท่ากับผลบวกของอีกสองด้าน
    Wrong: if it is exactly equal the shape is flat, not a triangle.ผิด: ถ้าเท่ากันพอดีจะแบนเป็นเส้นตรง ไม่ใช่สามเหลี่ยม
  • (B) The side is the longest oneด้านนั้นเป็นด้านที่ยาวที่สุด
    Wrong: the code checks every side, not just the longest.ผิด: โค้ดตรวจทุกด้าน ไม่ได้เลือกเฉพาะด้านยาวสุด
  • (C) The side is shorter than the sum of the other twoด้านนั้นสั้นกว่าผลบวกของอีกสองด้าน
    Correct! The test is sides[i2] + sides[i3] > sides[i1] for every i.ถูกต้อง! เงื่อนไขคือ sides[i2] + sides[i3] > sides[i1] สำหรับทุก i
  • (D) The side is an even numberด้านนั้นเป็นจำนวนคู่
    Wrong: a side length can be any number; even/odd is irrelevant.ผิด: ความยาวด้านเป็นจำนวนใดก็ได้ ไม่เกี่ยวกับคู่/คี่

ข้อ 4

4 / 10

Why use (i + 1) % N and (i + 2) % N as indices?ทำไมจึงใช้ (i + 1) % N และ (i + 2) % N เป็นดัชนี?

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

เฉลย / Answer: (D) So the “other two” side indices wrap back to the start of the listเพื่อให้ดัชนีของ “อีกสองด้าน” วนกลับมาต้นลิสต์

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

  • (A) To force the loop to run N timesเพื่อบังคับให้ลูปวน N รอบ
    Wrong: the number of rounds already comes from range(n).ผิด: จำนวนรอบมาจาก range(n) อยู่แล้ว
  • (B) To always skip side 0เพื่อข้ามด้านที่ 0 เสมอ
    Wrong: side 0 is still checked when i has other values.ผิด: ด้าน 0 ก็ถูกตรวจเมื่อ i เป็นค่าอื่น
  • (C) To round the side lengthsเพื่อปัดเศษความยาวด้าน
    Wrong: % is the remainder of a division, not rounding.ผิด: % คือเศษจากการหาร ไม่ใช่การปัดเศษ
  • (D) So the “other two” side indices wrap back to the start of the listเพื่อให้ดัชนีของ “อีกสองด้าน” วนกลับมาต้นลิสต์
    Correct! When i = 2 you get i2 = 0, i3 = 1 without running off the list.ถูกต้อง! เมื่อ i = 2 จะได้ i2 = 0, i3 = 1 ไม่หลุดขอบลิสต์

ข้อ 5

5 / 10

Why does calculate_area set A = S before the loop?ทำไม calculate_area จึงตั้ง A = S ก่อนเข้าลูป?

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

เฉลย / Answer: (A) So the first multiplication has a starting value; A ends up = s·(s−a)(s−b)(s−c)เพื่อให้การคูณครั้งแรกมีค่าตั้งต้น สุดท้าย A = s·(s−a)(s−b)(s−c)

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

  • (A) So the first multiplication has a starting value; A ends up = s·(s−a)(s−b)(s−c)เพื่อให้การคูณครั้งแรกมีค่าตั้งต้น สุดท้าย A = s·(s−a)(s−b)(s−c)
    Correct! A starts at S and is multiplied by (S - side) per side, matching Heron's formula.ถูกต้อง! A เริ่มที่ S แล้วคูณ (S - side) ทีละด้าน ได้ตามสูตรของเฮรอน
  • (B) To make A always an integerเพื่อให้ A เป็นจำนวนเต็มเสมอ
    Wrong: S comes from p / 2, so it is usually a decimal.ผิด: S มาจาก p / 2 จึงมักเป็นทศนิยม
  • (C) To count the number of sidesเพื่อนับจำนวนด้าน
    Wrong: counting sides uses len(sides), not A.ผิด: การนับด้านใช้ len(sides) ไม่ใช่ A
  • (D) To make the loop stop soonerเพื่อให้ลูปหยุดเร็วขึ้น
    Wrong: A's starting value has nothing to do with the loop count.ผิด: ค่าเริ่มต้นของ A ไม่เกี่ยวกับจำนวนรอบ

ข้อ 6

6 / 10

What does math.sqrt(A) do?math.sqrt(A) ทำอะไร?

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

เฉลย / Answer: (B) Returns the square root of Aคืนค่ารากที่สองของ A

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

  • (A) Rounds A to a whole numberปัดเศษ A ให้เป็นจำนวนเต็ม
    Wrong: rounding uses round(), not sqrt().ผิด: การปัดเศษใช้ round() ไม่ใช่ sqrt()
  • (B) Returns the square root of Aคืนค่ารากที่สองของ A
    Correct! After import math, math.sqrt(A) gives √A.ถูกต้อง! ต้อง import math ก่อน แล้ว math.sqrt(A) ให้ √A
  • (C) Squares Aยกกำลังสองของ A
    Wrong: squaring is A ** 2, the opposite of a square root.ผิด: การยกกำลังสองคือ A ** 2 ตรงข้ามกับรากที่สอง
  • (D) Returns the absolute value of Aคืนค่าสัมบูรณ์ของ A
    Wrong: absolute value is abs() or math.fabs().ผิด: ค่าสัมบูรณ์คือ abs() หรือ math.fabs()

ข้อ 7

7 / 10

If is_valid_data is False, what does the program print?ถ้า is_valid_data เป็น False โปรแกรมพิมพ์อะไร?

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

เฉลย / Answer: (C) the triangle sides are not validthe triangle sides are not valid

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

  • (A) The perimeter onlyเส้นรอบรูปเพียงอย่างเดียว
    Wrong: the else branch does not call calculate_perimeter.ผิด: สาขา else ไม่เรียก calculate_perimeter
  • (B) The area onlyพื้นที่เพียงอย่างเดียว
    Wrong: the else branch does not call calculate_area.ผิด: สาขา else ไม่เรียก calculate_area
  • (C) the triangle sides are not validthe triangle sides are not valid
    Correct! When the condition is false the else branch runs and prints this line.ถูกต้อง! เมื่อเงื่อนไขเป็นเท็จจะเข้า else ซึ่งพิมพ์ข้อความนี้
  • (D) Nothingไม่พิมพ์อะไร
    Wrong: the else branch always prints when the condition is false.ผิด: มีสาขา else ที่พิมพ์เสมอเมื่อเงื่อนไขเป็นเท็จ

ข้อ 8

8 / 10

If sides = [1, 2, 5], what happens?ถ้า sides = [1, 2, 5] จะเกิดอะไรขึ้น?

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

เฉลย / Answer: (D) 1 + 2 is not greater than 5, so it is not a triangle; it prints “the triangle sides are not valid”1 + 2 ไม่มากกว่า 5 จึงไม่เป็นสามเหลี่ยม พิมพ์ “the triangle sides are not valid”

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

  • (A) The area is about 4ได้พื้นที่ประมาณ 4
    Wrong: the area is never computed because the sides fail the check.ผิด: โปรแกรมไม่คำนวณพื้นที่เพราะด้านไม่ผ่านการตรวจสอบ
  • (B) It prints perimeter 8 and an areaพิมพ์เส้นรอบรูป 8 และพื้นที่
    Wrong: is_valid_triangle returns False, so the calculation branch is skipped.ผิด: is_valid_triangle คืน False จึงไม่เข้าสาขาคำนวณ
  • (C) It reports a good triangleพิมพ์ว่าเป็นสามเหลี่ยมที่ดี
    Wrong: the program has no such message.ผิด: โปรแกรมนี้ไม่มีข้อความแบบนั้น
  • (D) 1 + 2 is not greater than 5, so it is not a triangle; it prints “the triangle sides are not valid”1 + 2 ไม่มากกว่า 5 จึงไม่เป็นสามเหลี่ยม พิมพ์ “the triangle sides are not valid”
    Correct! When i points at the 5, sides[i2] + sides[i3] > sides[i1] is false and it returns False.ถูกต้อง! เมื่อ i ชี้ที่ด้าน 5 เงื่อนไข sides[i2] + sides[i3] > sides[i1] เป็นเท็จ return False

ข้อ 9

9 / 10

If sides = [3, 4, 5], what are the perimeter and the area?ถ้า sides = [3, 4, 5] เส้นรอบรูปและพื้นที่เป็นเท่าใด?

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

เฉลย / Answer: (B) 12 and 612 และ 6

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

  • (A) 12 and 1212 และ 12
    Wrong: the perimeter 12 is right, but the area is not 12.ผิด: เส้นรอบรูป 12 ถูก แต่พื้นที่ไม่ใช่ 12
  • (B) 12 and 612 และ 6
    Correct! 3+4+5 = 12, S = 6, A = 6·3·2·1 = 36, √36 = 6.ถูกต้อง! 3+4+5 = 12, S = 6, A = 6·3·2·1 = 36, √36 = 6
  • (C) 7 and 67 และ 6
    Wrong: the perimeter is the sum of all sides = 12, not 7.ผิด: เส้นรอบรูปคือผลรวมของทุกด้าน = 12 ไม่ใช่ 7
  • (D) 60 and 660 และ 6
    Wrong: 60 is 3×4×5 (the product), not the perimeter.ผิด: 60 คือ 3×4×5 (ผลคูณ) ไม่ใช่เส้นรอบรูป

ข้อ 10

10 / 10

Why split is_valid_triangle, calculate_perimeter, calculate_area into separate functions?ทำไมจึงแยก is_valid_triangle, calculate_perimeter, calculate_area เป็นฟังก์ชันย่อย?

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

เฉลย / Answer: (C) So each task is readable, testable on its own, and reusable, keeping the main flow shortเพื่อให้แต่ละงานอ่านง่าย ทดสอบแยกได้ และนำกลับมาใช้ซ้ำได้ ทำให้ flow หลักสั้น

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

  • (A) Because Python requires every loop to be in a functionเพราะ Python บังคับให้ทุกลูปอยู่ในฟังก์ชัน
    Wrong: loops can be outside functions (the input loop in main is outside).ผิด: ลูปอยู่นอกฟังก์ชันได้ (ลูปรับ input ในโปรแกรมหลักก็อยู่นอกฟังก์ชัน)
  • (B) Because functions always run faster than plain codeเพราะฟังก์ชันทำงานเร็วกว่าโค้ดปกติเสมอ
    Wrong: the main reason is clarity and reuse, not speed.ผิด: เหตุผลหลักคือความชัดเจนและการใช้ซ้ำ ไม่ใช่ความเร็ว
  • (C) So each task is readable, testable on its own, and reusable, keeping the main flow shortเพื่อให้แต่ละงานอ่านง่าย ทดสอบแยกได้ และนำกลับมาใช้ซ้ำได้ ทำให้ flow หลักสั้น
    Correct! calculate_area even reuses calculate_perimeter.ถูกต้อง! calculate_area ยังเรียก calculate_perimeter ซ้ำได้ด้วย
  • (D) Because a flowchart may not have more than one pageเพราะ flowchart ห้ามมีมากกว่า 1 หน้า
    Wrong: a flowchart can have several pages and sub-charts.ผิด: flowchart มีหลายหน้าและ sub-chart ได้