Preview

06 - Common Errors

 1. Spot the error (which line?) and explain what it is.
def main():
print("This is the main function")

main()

  Line 4 blank brackets error

  Line 2 indentation error

  Line 1 def main syntax error

  Line 3 blank space error

 2. Sometimes, the error is not on the line that your IDLE tells you it is on, but on the line before. Error checking would tell you there is an error on line 3. Is it right?
def main():
  x=int(input("Enter a number:")
  print(x)

main()

  No, the error is that the line before is missing a colon from the end

  Yes, in this case the error is on line 3

  No, the error is on line 2 (missing bracket at the end)

  No, the error is that the line before is too long

 3. Point out the line on which the error is.
score=20
if score>50
  print("You have passed!")
else:
  print("You have failed")

  Line 2 (missing colon :)

  Line 1 should be score==20 instead of score=20

  Line 4 should be indented further along to the right

  Line 3 should not be indented

 4. The following program will NOT run because the call to the main program (at the bottom) is not to the extreme left (which will cause an error)
def main():
	print("Hello World")
	
	
	
	main()

  True

  False

 5. What is the problem with this code snippet?
age=20
if age>12:
  print("You are eligible for a bank account")
  else:
    print("Sorry, too young")

  The problem is that the program discriminates against users that are over 20.

  There should not be a colon after the if statement in line 2

  The if and else should be on the same level of indentation (else should be moved back)

  There is no problem

 6. A runtime error occurs whenever the program instructs the computer to do something that it is either incapable or unwilling to do and causes a CRASH. An example is trying to divide by zero e.g. x=2 print(x/0)

  True

  False

 7. What is a Syntax error?

  This an error that is generated from outside of the universe

  This is an error in the spelling or grammar used when coding. e.g. PINT("hello") instead of print("hello")

  This is an error caused by the creator of the program

  This is an error that cannot be rectified EVER.

 8. LOGIC errors are the most difficult to fix. They occur when the program runs without crashing, but produces an incorrect result. The error is caused by a mistake in the program's logic but won't show up with any errors. Can you spot the logic error below?
x=3
y=5
z=x-y
print("Adding these two numbers will give you:",z)

  Line 4 will cause the program to crash

  Line 3 - the code will run, but produce the WRONG answer (e.g. -2 instead of 8)

  There are no logic errors in this that I can see

  Line 1 should have a colon after it

 9. The following code would cause WHAT type of Error on execution?
name=int(input("Enter your name:"))

  A HARDWARE ERROR: This would cause the CPU fan to whir out of control

  NO ERROR: Error? What error?

  A DARING ERROR: It is asking the user for their name - how dare it!?

  VALUE ERROR: It is asking for a string but is set up to receive integer input

 10. Some words in programming languages are RESERVED WORDS, which means you should not use them as variable names. What's the problem with the code below?
print=10
print(print)

  '10' is a reserved number so can never be used in Python

  "print" is a reserved word, so this will cause an error.

  There is no problem

  Line 2 should be indented