# Logic errors in practice

## Boolean errors and missing steps

Imagine that a 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 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 contains several that will cause the program to behave unexpectedly:

• Line 2 contains the wrong 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 . 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 * 4```

The code contains several logic errors that will cause the program to behave unexpectedly:

• Line 1 contains a 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 by 4. We can only perform calculations on numbers, not strings.
• Lines 2, 3 and 4 are in the wrong . 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)```