Tag: Input Validation

National 5 – Algorithm Specification (Revision)

Input validation – checking that what is entered by the user is acceptable.

The following program checks that age is a positive number.

1. SEND “Please enter age” TO DISPLAY
3. RECEIVE age FROM (INTEGER) KEYBOARD
4. WHILE age < 0 DO
5.         SEND “re-enter data” TO DISPLAY
6.         RECEIVE age FROM (INTEGER) KEYBOARD
7. END WHILE

Note that acceptable does not mean accurate, just sensible.

N5 & Higher – Input Validation (Python)

Input validation is used in both National 5 and at Higher. It consists of 3 parts.

  1. Get the user to enter a value
  2. While the value is wrong
  3.     Get the user to enter a value

The use of the while construct allows the program to repeatedly ask the user to enter valid data.

1
2
3
4
5
6
7
8
9
age=0

#Input Validation
age=int(input('Please enter your age>'))
while age<0 or age>120: # Check that age is between 0 and 120
    print('Please check your age is between 0 and 120')
    age=int(input('Please enter your age>'))
    
print('Welcome')

This can also be used with strings, can you work out how to modify the code so that it would only take a ‘Yes’ ‘No’ answer?