Putting it all together
Putting it all together
- You now know variables, input,
if, loops, lists, functions, and dictionaries. - A real program combines these small pieces into one useful tool.
- In this lesson you build a tiny class-scores analyzer, one function at a time.
รวมทุกอย่างเข้าด้วยกัน
- ตอนนี้คุณรู้จักตัวแปร, Input,
if, ลูป, ลิสต์, ฟังก์ชัน, และ dictionary แล้ว - โปรแกรมจริงจะรวมชิ้นส่วนเล็กๆ เหล่านี้เข้าด้วยกันเป็นเครื่องมือที่มีประโยชน์
- ในบทเรียนนี้เราจะสร้าง ตัววิเคราะห์คะแนนห้องเรียน ขนาดเล็ก โดยเริ่มทีละฟังก์ชัน
The plan
- We have a list of test scores, like
[70, 90, 50, 80]. - We want three answers: the average, how many passed, and a short summary.
- We will write one small function for each job, then they work together.
แผนการทำงาน
- เรามี ลิสต์ ของคะแนนสอบ เช่น
[70, 90, 50, 80] - เราต้องการคำตอบสามอย่าง: ค่า เฉลี่ย, จำนวนคนที่ ผ่าน, และ สรุป สั้นๆ
- เราจะเขียน ฟังก์ชัน เล็กๆ สำหรับแต่ละงาน แล้ว让它们ทำงานร่วมกัน
The analyzer calls each helper
A big program calls small functions in turn, each returning one answer. · โปรแกรมใหญ่เรียกฟังก์ชันย่อยตามลำดับ โดยแต่ละฟังก์ชันจะตอบกลับหนึ่งคำตอบ
Handy built-ins for lists
- Python gives you ready-made helpers that work on a list of numbers.
len(x)counts the items,sum(x)adds them,max(x)/min(x)find the largest / smallest.- Use them instead of writing a loop every time.
ฟังก์ชัน built-in ที่มีประโยชน์สำหรับลิสต์
- Python มีตัวช่วยสำเร็จรูปที่ทำงานกับลิสต์ตัวเลขได้
len(x)นับจำนวนรายการ,sum(x)รวมค่าทั้งหมด,max(x)/min(x)หาค่ามากสุด/น้อยสุด- ใช้พวกมันแทนการเขียนลูปทุกครั้ง
scores = [70, 90, 50, 80]
print(len(scores)) # 4
print(sum(scores)) # 290
print(max(scores)) # 90
print(min(scores)) # 50
A pattern you will reuse: count items that pass a test
- A very common job: loop a list, and count the items that pass some test.
- Start a counter at
0, and add1each time theifis true. - Here we count the long words; soon you will count the passing scores.
รูปแบบที่คุณจะนำไปใช้ซ้ำ: นับรายการที่ผ่านเกณฑ์
- งานที่พบบ่อยมาก: ลูปผ่านลิสต์ แล้ว นับ รายการที่ผ่านเงื่อนไขบางอย่าง
- เริ่มตัวนับที่
0และเพิ่ม1ทุกครั้งที่ifเป็นจริง - ที่นี่เรานับคำยาว;เร็วๆ นี้คุณจะนับคะแนนที่ผ่าน
def how_many_long_words(words, min_length):
count = 0
for w in words:
if len(w) >= min_length:
count = count + 1
return count
print(how_many_long_words(["hi", "hello", "hey", "howdy"], 4)) # 2
Common mistakes
- Build in small pieces and test each one before adding the next.
- Read the whole task first, then plan before you code.
ข้อผิดพลาดที่พบบ่อย
- สร้างเป็นชิ้นส่วนเล็กๆ และทดสอบแต่ละชิ้นก่อนเพิ่มส่วนถัดไป
- อ่านโจทย์ทั้งหมดก่อน แล้ววางแผนก่อนเริ่มเขียนโค้ด
Now you try
- Build the analyzer in three steps:
average,count_passing, thenstats. - Each function is small. Together they turn a list of scores into real answers.
- Press Check answer after each one.
ลองดูเลย
- สร้างตัววิเคราะห์เป็นสามขั้นตอน:
average,count_passing, แล้วstats - แต่ละฟังก์ชันมีขนาดเล็ก เมื่อรวมกันจะเปลี่ยนลิสต์คะแนนให้เป็นคำตอบที่แท้จริง
- กด Check answer หลังเสร็จแต่ละส่วน
Write average(scores) that returns the average (mean) of a list of numbers: the total divided by how many. If the list is empty, return 0 (so it does not crash). Example: average([2, 4]) is 3.0. · เขียน average(scores) ที่คืนค่าเฉลี่ย (mean) ของรายการตัวเลข: ผลรวมหารด้วยจำนวนตัว. หากรายการ ว่าง, คืนค่า 0 (เพื่อให้ไม่เกิดข้อผิดพลาด). ตัวอย่าง: average([2, 4]) คือ 3.0.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write count_passing(scores, pass_mark) that returns how many scores are greater than or equal to pass_mark. Loop the list and count. Example: count_passing([40, 50, 60], 50) is 2. · เขียน count_passing(scores, pass_mark) ที่คืนจำนวนคะแนนที่ มากกว่าหรือเท่ากับ pass_mark. ลูปผ่านลิสต์แล้วนับ. ตัวอย่าง: count_passing([40, 50, 60], 50) จะคืนค่าเป็น 2.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write stats(scores) (assume at least one score) that returns a dictionary with three keys: "highest", "lowest", and "average". Example: stats([70, 90, 50, 80]) returns {"highest": 90, "lowest": 50, "average": 72.5}. · เขียน stats(scores) (สมมติว่ามีคะแนนอย่างน้อยหนึ่งค่า) ที่คืนค่า ไดคชั่นนารี ที่มีสามคีย์: "highest", "lowest" และ "average". ตัวอย่าง: stats([70, 90, 50, 80]) คืนค่า {"highest": 90, "lowest": 50, "average": 72.5}.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write grade_counts(scores) that returns a dictionary counting grades: A for 80 and above, B for 60 and above, C for the rest. Always include all three keys: grade_counts([85, 72, 90, 40]) is {"A": 2, "B": 1, "C": 1}. · เขียน grade_counts(scores) ที่ คืนค่า ไดคชั่นนนับเกรด: A สำหรับ 80 ขึ้นไป, B สำหรับ 60 ขึ้นไป, C สำหรับส่วนที่เหลือ. ต้องมีทุกคีย์ทั้งสามเสมอ: grade_counts([85, 72, 90, 40]) คือ {"A": 2, "B": 1, "C": 1}.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่