Our site uses cookies. Some of the cookies we use are essential for parts of the site to operate and have already been set. You may delete and block all cookies from this site, but parts of the site will not work. To find out more about cookies on this website, see our Cookie Policy
Accept
© eRevision.uk and ZigZag Education 2025
This test is run by .
Note that your final mark will not be saved in the system.

Producing robust programs (2.3) GapFill

Target Level
4-5
Running Total
0
0%
Attempt
1 of 3

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 = 
                
                
                 charintconvertstr(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) 
                
                
                 <===ORAND isInteger(upperLimit)}
   lowerLimit = int(lowerLimit)
   upperLimit = int(upperLimit)
   if upperLimit 
                
                
                 <=>=NOTOR 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 
                
                
                 int(lowerLimit)isInteger(upperLimit)isInteger(lowerLimit)int(lowerLimit) <= int(upperLimit)
      lowerLimit = int(lowerLimit)
      inputSet = True
   else
      print("The number that you entered is not valid. Please try again.")
   endif
until 
                
                
                 inputSetfalseupperLimit > lowerLimitlowerLimit < upperLimit
do
   inputSet = False
   upperLimit = input("Enter the highest value that can be given"))
   if isInteger(upperLimit) AND (
                
                
                 int(upperLimit)int(upperLimit) >= lowerLimitisInteger(lowerLimit)upperLimit > lowerLimit)
      upperLimit = int(upperLimit)
      inputSet = True
   else
      print("The number that you entered is not valid. Please try again.")
   endif
until 
                
                
                 inputSetlowerLimit < upperLimitfalselowerLimit > upperLimit
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 
                
                
                 isInteger(lowerLimit)|NOT isInteger(lowerLimit)NOT isInteger(upperLimit)lowerLimit >= upperLimitisInteger(upperLimit)
         print("You must enter a numeric value (e.g. 16). Please try again.")   
      endif
      if 
                
                
                 NOT isInteger(lowerLimit)lowerLimit >= upperLimitlowerLimit > upperLimitisInteger(lowerLimit)
         print(
                
                
                 "You cannot enter a number lower than " + lowerLimit + ".""An error has occurred. Please try again.""You cannot enter a number lower than " + lowerLimit + ". Please try again.""An error has occurred.")
      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  data validationexception handlingdata verificationthrowing an error. 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  data verificationthrowing an errorlogic error correctiondata validation

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:  syntaxlogicinputoutput 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  outputinputsyntaxlogic errors, which are accepted as valid code, but cause the program to do something that the programmer did not intend.

This is your 1st attempt! You get 3 marks for each one you get right. Good luck!

Pass Mark
72%