Validation & verification
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
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.
English
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.
English
month = 13
if 1 <= month <= 12:
print("Accepted")
else:
print("Rejected")
English
Length check
- A length check makes sure text is not too short or too long.
len(text)gives the number of characters.
English
password = "abc"
if len(password) >= 8:
print("Accepted")
else:
print("Too short")
English
Presence check
- A presence check makes sure something was actually entered.
- An empty string
""means the user typed nothing.
English
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.
English
In the exam: types of check
- Know these validation checks: range, length, type, presence, format, check digit.
IF Age >= 0 AND Age <= 120
THEN
OUTPUT "Valid"
ELSE
OUTPUT "Invalid"
ENDIF
English
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.
English
Now you try
- Each function returns
Trueto accept orFalseto reject. - Press Check answer to test your code.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write valid_age(a) that returns True when a is a sensible age (0 to 120 inclusive), otherwise False. This is a range check.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write valid_password(p) that returns True when the password is at least 8 characters long. This is a length check.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write is_present(s) that returns True when something was entered, and False for an empty string "". This is a presence check.
คลิก Run เพื่อดูผลลัพธ์ที่นี่