Computer Science

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Computer science

● A syntax error is a spelling or grammatical mistake in the program.


A mistake in logic is logic error.

● A logic error is a mistake in the logic of the program. A syntax error


is a spelling or grammatical mistake in the program.

● A comment is a sentence that explains what a section of code is


trying to do.

● A variable is location in memory that is used to store data

name = input("What is your name? ")


pront("Hello, " + name)

The statement print is spelled incorrectly. The program will execute the
first instruction correctly then crash when it reaches the error.
The correct code is:
name = input("What is your name?")
print("Hello, " + name)

Syntax errors can be tricky to spot. Consider this short Python program:
forename = input("What is your forename? ")
surname = input("What is your surname? ”)
print "Hello, " + forname + " " + surname)

The code contains two errors, this time both in line 3:


● The first is a punctuation syntax error. A bracket is missing after
the print statement.
● The second is a variable syntax error. The variable ‘forename’ is
spelt incorrectly.

The program will execute the first two instructions correctly then crash
when it reaches the first error in line 3.
The correct code is:
forename = input("What is your forename? ")
surname = input("What is your surname? ")
print("Hello, " + forename + " " + surname)

Fixing syntax errors is simple. Spotting them is more difficult. Some of


the most common syntax errors are things that come in pairs. Always
check for speech marks and brackets first if your program crashes.

Imagine that a program 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 Python 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 code contains several logic errors that will cause the program to
behave unexpectedly:
● Line 2 contains the wrong Boolean 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 instruction. 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.

You might also like