Note that your final mark will not be saved in the system.
Producing robust programs (2.3) GapFill
You must fill all the gaps before clicking ‘Check Answers!’
It can be hard to predict every input that a program will need to process, especially when user input is required. When designing a program, it is useful to think about the different situations that your program should be able to handle, as others are unlikely to use your program in the exact way that you intend them to.
Consider the following program that allows a user to generate a random integer number between two values:
lowerLimit =
(input("Enter the lowest value that can be given"))
upperLimit = int(input("Enter the highest value that can be given"))
print("The result is: " + randInt(lowerLimit, upperLimit))
This works fine if the user enters the correct inputs. But if they enter a string, such as 'four', then the program will crash when it tries to convert the input into an integer. It would also crash if the first number entered is larger than the second number entered. To make sure that this doesn't happen, we can add some checks to make sure that the input can be used before we try to convert it or pass it to the randInt function.
lowerLimit = input("Enter the lowest value that can be given"))
upperLimit = input("Enter the highest value that can be given"))
if isInteger(lowerLimit)
isInteger(upperLimit)}
lowerLimit = int(lowerLimit)
upperLimit = int(upperLimit)
if upperLimit
lowerLimit}
print("The result is: " + randInt(lowerLimit, upperLimit))
else
print("The numbers that you entered are not valid")
endif
else
print("The numbers that you entered are not valid")
endif
This ensures that an error won't occur. However, we can improve the program further by changing the program so that it keeps asking the user for each input until it receives a valid response.
do
inputSet = False
lowerLimit = input("Enter the lowest value that can be given"))
if
lowerLimit = int(lowerLimit)
inputSet = True
else
print("The number that you entered is not valid. Please try again.")
endif
until
do
inputSet = False
upperLimit = input("Enter the highest value that can be given"))
if isInteger(upperLimit) AND (
)
upperLimit = int(upperLimit)
inputSet = True
else
print("The number that you entered is not valid. Please try again.")
endif
until
print("The result is: " + randInt(lowerLimit, upperLimit))
If you wanted to make the program even more user-friendly, you could add more selection so that the error message that is displayed lets the user know what the issue is
do
...
if isInteger(lowerLimit)
...
else
print("You must enter a numeric value (e.g. 16). Please try again.")
endif
until inputSet
do
...
if isInteger(upperLimit) AND (int(upperLimit) >= lowerLimit)
...
else
if
print("You must enter a numeric value (e.g. 16). Please try again.")
endif
if
print(
)
endif
endif
until inputSet
print("The result is: " + randInt(lowerLimit, upperLimit))
The final program is more complicated than the original program, but it is now much more convenient for the user. It is usually worth the extra effort to make your program more robust, because it will save a lot of time when the program is being used.
The changes made to the program above ensure that the user input that is provided can be used by the system, which is known as . It is also good practice to include checks to make sure that the user has given the information that they intended to give, which is known as
As well as handling any errors that may be introduced by user input, you should also be aware of errors that may be introduced when developing a program. There are two general types of error that can be made: errors, that occur when the programming language doesn't recognise a command which it has been given, causing the program to fail to be translated into machine code; and errors, which are accepted as valid code, but cause the program to do something that the programmer did not intend.