Input validation is used in both National 5 and at Higher. It consists of 3 parts.
- Get the user to enter a value
- While the value is wrong
- 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?