Logic errors in practice
Boolean errors and missing steps
Imagine that a programSequences of instructions for a computer. is required to determine if we are eligible for discounted tickets at the cinema, when discounted tickets are available for people aged 15 or under and those who are 65 or over. The program must:
- Check to see if we are 65 or over. If so, the program must say “Eligible for a discount.”.
- Check to see if we are 15 or under. If so, the program must say “Eligible for a discount.”.
- Say “Not eligible for a discount.” if we are any other age.
Look at this PythonA high-level programming language. program:
age = int(input("How old are you? "))
if age == 65: print("Eligible for a discount.")
elif age >= 15: print("Eligible for a discount.")The codeInstructions in a computer program. contains several Logic errorA fault in the logic or structure of the problem. that will cause the program to behave unexpectedly:
- Line 2 contains the wrong BooleanA data type in computing which only has two possible values, true or false. expression. The program asks if we are aged exactly 65, not if we are 65 or over. Therefore, if we are 65 exactly, we will get a discount. If we are older than 65, no message will be displayed.
- Line 4 also contains the wrong Boolean expression. The program asks if we are 15 or older. It should actually ask if we are 15 or under.
- There is a missing instructionA single action that can be performed by a computer processor.. If we are not 65 or over or 15 and under, the program does not tell us that we are not eligible for a discount.
The correct program would look like this:
age = int(input("How old are you? "))
if age >= 65: print("Eligible for a discount.")
elif age <= 15: print("Eligible for a discount.")
else: print("Not eligible for a discount.")It could even look like this:
age = int(input("How old are you? ")) if age >= 65 or age <= 15: print("Eligible for a discount.")
else: print("Not eligible for a discount.")Either program would give us the correct result.
Sequence errors and incorrect data types
Imagine a program that is required to calculate the perimeter of a square. The program must:
- allow the user to input the length of a side
- calculate the perimeter
- output the perimeter
Look at this Python program:
length = input("Enter the length of one side: ")
print("Perimeter of your square is: ")
print(perimeter)
perimeter = length * 4The code contains several logic errors that will cause the program to behave unexpectedly:
- Line 1 contains a data typeThe format in which a variable or constant holds data, such as ‘integer’ or ‘string’. error. Side length should be a number, but in Python you need to say
int(input(...))or else the program will think you are typing text. When line 4 comes to calculate the perimeter, an error will occur because we will try to multiply a stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. by 4. We can only perform calculations on numbers, not strings. - Lines 2, 3 and 4 are in the wrong sequenceIn computer programming, this is a set of instructions that follow on one from another.. We can’t output the perimeter until we’ve calculated it.
The correct program would look like this:
length = int(input("Enter the length of one side: "))
perimeter = length * 4
print("Perimeter of your square is: ")
print(perimeter)