Validation & verification · การตรวจสอบและยืนยันผล
Why validate input
- Validation checks that input is sensible before the program uses it.
- "Garbage in, garbage out" — bad data leads to wrong answers.
- The program decides to accept or reject the value.
ทำไมต้องตรวจสอบข้อมูลนำเข้า
- การตรวจสอบความถูกต้อง ตรวจสอบว่าข้อมูลเข้ามี ความสมเหตุสมผล ก่อนที่โปรแกรมจะนำไปใช้
- "ข้อมูลผิดพลาด นำไปสู่ผลลัพธ์ที่ผิดพลาด" — ข้อมูลที่ไม่ดีจะนำไปสู่คำตอบที่ผิด
- โปรแกรมจะตัดสินใจ ยอมรับ หรือ ปฏิเสธ ค่านั้น
The validation steps · ขั้นตอนการตรวจสอบ
Read the input, check it, then accept or reject — bad data never gets used. · อ่านข้อมูลเข้า, ตรวจสอบ, แล้วยอมรับหรือปฏิเสธ — ข้อมูลที่ไม่ดีจะไม่ถูกนำไปใช้
Range check
- A range check makes sure a number is between a low and a high limit.
- Example: a month must be from 1 to 12.
การตรวจสอบช่วงค่า
- การตรวจสอบช่วง ตรวจสอบให้แน่ใจว่าตัวเลขอยู่ในระหว่างค่าต่ำสุดและค่าสูงสุดที่กำหนด
- ตัวอย่าง: เดือนต้องอยู่ระหว่าง 1 ถึง 12
month = 13
if 1 <= month <= 12:
print("Accepted")
else:
print("Rejected")
Length check
- A length check makes sure text is not too short or too long.
len(text)gives the number of characters.
การตรวจสอบความยาว
- การตรวจสอบความยาว (length check) ตรวจสอบให้แน่ใจว่าข้อความไม่สั้นหรือยาวเกินไป
len(text)gives the number of characters.
password = "abc"
if len(password) >= 8:
print("Accepted")
else:
print("Too short")
Presence check
- A presence check makes sure something was actually entered.
- An empty string
""means the user typed nothing.
การตรวจสอบการมีอยู่
- การตรวจสอบการมีอยู่ (presence check) ตรวจสอบให้แน่ใจว่ามีข้อมูลถูกป้อนเข้าจริงๆ
- ตัวแปรข้อความว่าง
""หมายความว่าผู้ใช้ไม่ได้พิมพ์อะไรเลย
Verification: checking the copy
- Verification checks that data was copied or typed correctly.
- A double-entry check asks for the value twice and compares them.
- A visual check means a human reads it back on screen.
การตรวจสอบ: การตรวจสอบสำเนา
- การตรวจสอบ (verification) ตรวจสอบว่าข้อมูลถูก คัดลอกหรือพิมพ์ถูกต้อง
- การตรวจสอบด้วยการป้อนซ้ำ (double-entry check) ขอค่าเดียวกันสองครั้งแล้วเปรียบเทียบกัน
- การตรวจสอบด้วยสายตา (visual check) หมายความว่าบุคคลหนึ่งอ่านกลับเข้าไปบนหน้าจอ
In the exam: types of check
- Know these validation checks: range, length, type, presence, format, check digit.
ในการสอบ: ประเภทของการตรวจสอบ
- รู้จักการตรวจสอบ การ-valid-เทชัน (validation) เหล่านี้: ช่วง, ความยาว, ประเภท, การมีอยู่, รูปแบบ,的数字校验
IF Age >= 0 AND Age <= 120
THEN
OUTPUT "Valid"
ELSE
OUTPUT "Invalid"
ENDIF
Common mistakes
- Validation checks data is sensible (range, length, type); verification checks it was entered correctly.
- A range check needs both a lower and an upper limit.
ข้อผิดพลาดที่พบบ่อย
- การ-valid-เทชัน ตรวจสอบว่าข้อมูลสมเหตุสมผล (ช่วง, ความยาว, ประเภท); การตรวจสอบ (verification) ตรวจสอบว่าป้อนได้ถูกต้อง
- การตรวจสอบช่วง ต้องมีทั้งค่าต่ำสุดและค่าสูงสุด
Now you try
- Each function returns
Trueto accept orFalseto reject. - Press Check answer to test your code.
ลองดูเลย
- ฟังก์ชันแต่ละตัวจะคืนค่า
Trueเพื่อยอมรับ หรือFalseเพื่อปฏิเสธ - กด Check answer เพื่อทดสอบโค้ดของคุณ
Write valid_age(a) that returns True when a is a sensible age (0 to 120 inclusive), otherwise False. This is a range check. · เขียน valid_age(a) ที่ส่งกลับ True เมื่อ a เป็นอายุที่เหมาะสม (ระหว่าง 0 ถึง 120 รวมขอบเขต), มิฉะนั้นส่งกลับ False. นี่คือ การตรวจสอบช่วง
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write valid_password(p) that returns True when the password is at least 8 characters long. This is a length check. · เขียน valid_password(p) ที่ส่งกลับ True เมื่อรหัสผ่านมีความยาวอย่างน้อย 8 ตัวอักษร. นี่คือ การตรวจสอบความยาว
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write is_present(s) that returns True when something was entered, and False for an empty string "". This is a presence check. · เขียน is_present(s) ที่ส่งกลับ True เมื่อมีการป้อนข้อมูล และส่งกลับ False สำหรับสตริงว่าง "". นี่คือ การตรวจสอบการมีอยู่
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่