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 2 indentation error

  Line 3 blank space error

  Line 1 def main syntax error

  Line 4 blank brackets 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()

  Yes, in this case the error is on line 3

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

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

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

 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 4 should be indented further along to the right

  Line 2 (missing colon :)

  Line 3 should not be indented

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

 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()

  False

  True

 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)

  False

  True

 7. What is a Syntax error?

  This is an error caused by the creator of the program

  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 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 1 should have a colon after it

  Line 4 will cause the program to crash

  There are no logic errors in this that I can see

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

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

  NO ERROR: Error? What error?

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

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

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

 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)

  Line 2 should be indented

  '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