Validation and authentication, syntax and logic errors, test data and test plans, and code that is easy to maintain.
Defensive design
A robust program copes with whatever users type.
- Validation checks that input is sensible before it is used: range (from 1 to 12), length (at least 8 characters), presence (not left empty), format (looks like a postcode), type (a whole number).
- Authentication checks that a user is who they claim to be, for example a username and password.
- Maintainability: comments, meaningful names, indentation and subroutines make code easier to fix later.
A validation loop: it repeats until the value is in range
n = int(input("Enter a month number"))
while n < 1 OR n > 12
print("Invalid")
n = int(input("Enter a month number"))
endwhile
print("Accepted " + str(n))n ← STRING_TO_INT(USERINPUT)
WHILE n < 1 OR n > 12
OUTPUT "Invalid"
n ← STRING_TO_INT(USERINPUT)
ENDWHILE
OUTPUT "Accepted " + INT_TO_STRING(n)n = int(input("Enter a month number"))
while n < 1 or n > 12:
print("Invalid")
n = int(input("Enter a month number"))
print("Accepted " + str(n))The rest of this lesson and its 7 practice questions are in Plus.
Screen 1 of 3, free to read.
- 7 exam-style questions, marked as you go, each with a worked solution
- Anything you get wrong is saved to your flashcards