0% found this document useful (0 votes)
38 views

T4 Homework 4

This document discusses syntax errors, logic errors, and the efficiency of algorithms. It provides an example of a program with a syntax error on line 6 and explains how to fix it. A syntax error occurs when the code breaks grammatical rules of the language. It also gives an example of a logic error, which produces unexpected output due to flawed logic. Unlike syntax errors, logic errors don't prevent a program from running. Finally, it compares two algorithms for calculating the perimeter of a square. The second algorithm is more efficient because it has no iterations and uses fewer variables, requiring less processing and memory than the first.

Uploaded by

maxvince2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

T4 Homework 4

This document discusses syntax errors, logic errors, and the efficiency of algorithms. It provides an example of a program with a syntax error on line 6 and explains how to fix it. A syntax error occurs when the code breaks grammatical rules of the language. It also gives an example of a logic error, which produces unexpected output due to flawed logic. Unlike syntax errors, logic errors don't prevent a program from running. Finally, it compares two algorithms for calculating the perimeter of a square. The second algorithm is more efficient because it has no iterations and uses fewer variables, requiring less processing and memory than the first.

Uploaded by

maxvince2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Homework 4 Trace tables and errors

Unit 1 Computational thinking


Name:............................................................................. Class:................... Mark:................

1. Look at the following program which validates a password:

1 password = "EpCg3E4("
2 validPassword = False
3 while not validPassword:
4 userPass = input("Enter the password: ")
5 if userPass == password:
6 print "Valid password")
7 validPassword = True
8 else:
9 print("Invalid password")

(a) The program has a syntax error in line 6.


Explain what is meant by the term syntax error.
The term syntax error are errors which break the grammatical rules of a programming
language. This stops the program from being ran or translated

(b) Rewrite line 6 to fix the syntax error.


Print(“Valid password”)

(c) Explain one other type of error that a programmer could make when programming.
Logic errors are errors which produce an unexpected output due to a mistake made in the
logic of the programming.

However, this type of error does not stop the program from running.

1
Homework 4 Trace tables and errors
Unit 1 Computational thinking

2. Look at the following algorithm that calculates the perimeter of a square:

length = int(input("Enter length"))


perimeter = 0
for i in range(4):
perimeter = perimeter + length
print(perimeter)

(a) Complete the trace table below for an input of 25.

length i perimeter Output

25 25, 25, 25, 25 25 25, 25, 25, 25

(b) A second algorithm is written as follows:

length = int(input("Enter length"))


perimeter = 4 * length
print(perimeter)

Explain two reasons why the second algorithm is more efficient than the first.
There are no iterations in the second algorithm while the first algorithm has 3 iteration of its

Program.

This makes the second algorithm require less processing and more efficient,

The first algorithm uses 3 variables while the second algorithm uses 2 variables.

This makes the second algorithm more efficient in terms of memory.

2
Homework 4 Trace tables and errors
Unit 1 Computational thinking
[Total 14 Marks]

You might also like