Input Validation

Input validation is used to ensure the user can only type in valid values.

See also pages on decisions (if commands) and conditions.

General Algorithm

get input from user
while input is not valid
    display error
    get input from user
end while

Length Check

User must enter a string that has a minimum/maximum number of characters.

#
computer = input('Enter computer ID:')
while len(computer) != 14:
    print('Must have 14 characters')
    computer = input('Enter computer ID:')
#

Range Check

User must enter a number that is bigger/smaller than another number.

# 
quantity= input('Enter quantity:') 
while quantity < 1: 
    print('Must be at least 1') 
    quantity= input('Enter quantity:') 
#

Restricted Choice

User must enter a value that is in a list of valid strings or numbers

#
happy = input('Are you happy?')
while happy !='Yes' and happy!='No': 
    print('Enter Yes or No')
    happy = input('Are you happy?')
#