# Debugging and refining an algorithm – Q&A

Question 1[4 marks]
Zara has created the following program in her computer science lesson. The aim of this program is to find out if the user is old enough to vote or not, the voting age being 18 years old. If the user is too young to vote, the program will calculate and output in how many years the user will be allowed to vote!

Here is her Python code:
```#Voting Age Checker
a = int(input("How old are you?"))

if a<=18:
print(You can vote!")
else:
y = 18 - a
print("You will be allowed to vote in " + str(y) + " years!")
```

Zara made two errors in her code. A syntax error and a logic error. Identify these two errors and explain how these could be fixed.
Syntax Error:

Logic Error:

Question 2[2 marks]
Explain the difference between a logic error and a syntax error.
Question 3[3 marks]
In order to test her program, Zara is designing a test plan. She wants to use specific input values to complete three types of tests:
• Normal/Valid Test
• Boundary/Borderline/Extreme Test
• Erroneous Test
Identify three input values that Zara could use in her test plan.
• Valid/Normal Test Data:

• Boundary Test Data:

• Erroneous Test Data:

Question 4[2 marks]
Identify two purposes of testing.
Question 5[2 marks]
Identify two ways, Zara could use to make her code easier to understand and maintain.
Question 6[5 marks]
Zara would like to create a function using her code. The function should be called checkVotingAge(). It should
• take one parameter (the age of the user as an integer value),
• output the messages as it currently does
• return a Boolean value as to whether the user is allowed to vote or not.
Rewrite Zara’s code into a function.
[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

Question 1Question 2Question 3Question 4Question 5Question 6

#### Question 1

Syntax Error:
Missing speechmarks – On line 5 – print(“You can vote!”)

Logic Error:
Wrong comparison operator on line 4: it should be: if a>=18:

#### Question 2

With a logic error, the code does compile and run but it does not produce the expected output.
With a syntax error, the code does not compile. A syntax error is raised preventing the code to be executed.

#### Question 3

Valid Test Data:
Any positive integer such as 7, 12, 24, 56

Boundary/Borderline/Extreme Test Data:
18

Erroneous Test Data:
An empty input, or some characters such as “eighteen”, “abc#!”

#### Question 4

To check that the code is working as expected and producing the expected output for some given inputs.
To spot any issues/bugs in the code.

#### Question 5

She could use more meaningful identifiers for her variables such as age instead of a.
She could use annotations to explain different sections of the code.

#### Question 6

```#In Python:
def checkVotingAge(age):
if age>=18:
print("You can vote!")
return True
else:
years = 18 - age
print("You will be allowed to vote in " + str(years) + " years!")
return False```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 47

No votes so far! Be the first to rate this post.

As you found this challenge interesting...