7_3_6

WHAT: Use logical reasoning to detect and correct errors in programs

HOW:

Activity 1

Before you begin this outcome, you might want to refresh yourself on outcome 7_2_5 where you detected errors in algorithms.

Activity 2

Fix the Logic Errors!

Below is some code and it has some logic errors in it. Can you spot and fix all four?

# LOGIC ERRORS ARE TRICKY TO FIND
# THE CODE APPEARS TO RUN, BUT NOT IN THE WAY THAT WE EXPECT IT TO
# SPOT THE LOGIC ERRORS IN THIS CODE AND FIX THEM

# ERROR 1 - This SHOULD print the numbers 1 to 10
x=0
while x < 10:
  print (x)
  x=x+1

# ERROR 2 - This SHOULD say HELLO when you type in yes

answer=str(input("Are you here? "))

if answer!="yes":
  print ("HELLO")

# ERROR 3 - This SHOULD say HIGHER when someone types a 1

ans=int(input("Type a number : "))

if ans<1:
  print ("HIGHER")

# ERROR 4 - This SHOULD check the password, then load IF it's correct

password="enter"
checkPass=str(input("Enter password to load program :"))

if password<checkPass:
  print ("...program loading")

Activity 3

Fix the syntax errors!

Below is some code and it has some syntax errors in it. Can you spot and fix all five?

# SYNTAX ERRORS HAPPEN WHEN THE CODE DOESN'T UNDERSTAND YOU
# YOU MIGHT HAVE MISSED A SEMI-COLON OR A BRACKET
# YOU MAY HAVE WRITTEN A VARIABLE NAME INCORRECTLY
# YOU MAY NOT HAVE INDENTED CORRECTLY

# THE CODE BELOW WILL NOT RUN UNTIL YOU FIX ALL FIVE ERRORS

answer=input("Type your name here :"
if anwser=="ben"
  print "Hi BEN!")
  peint ("It is a lovely day today")

Activity 4

Fix the runtime errors!

Below is some code and it has some runtime errors in it. Can you spot and fix all three?

WARNING: A run time error will keep running and running the code. You will need to scroll up to the top of the running code to spot what is going wrong.

# RUN TIME ERRORS CAUSE PROBLEMS WHEN THE CODE IS RUNNING
# THEY CAN CRASH THE COMPUTER
# THEY SOMETIMES HAPPEN WHEN CODE GETS STUCK IN A LOOP
# OR A CALCULATION MAY BE TOO LARGE FOR THE COMPUTER TO DEAL WITH IT

# SPOT AND FIX THE RUNTIME ERRORS IN THE CODE BELOW

# ERROR 1 - This program SHOULD print the numbers 1 to 10

x=0
while x<11:
  print (x)

# ERROR 2 - This program SHOULD stop running at 20

y=0
while y!=20: 
  print (y)
  y=y+3

# ERROR 3 - This should multiply any number entered by 6

number=str(input("Please enter a number : "))
number=number*6
print (number)

CHECK:

EMBED:

1. Write about what might happen if you created an application that had lots of errors in it and sold it to customers.

2a. Find out what happened when the “Ariane 5” had its first test flight.

2b. How could error checking help?

CLASSROOM IDEAS:

The trickiest activity on here is Activity 4, the runtime errors. This is because when the code runs it zooms through loads of outputs. I would show the students that this happens and how to scroll to the top to avoid lots of hands up!