Challenge 1 +50 XP
The following code has a syntax error. Fix it so it prints 'Hello'.
- Test 1 — expects "Hello\n"
Need a hint? (−25% XP)
Make sure every opening parenthesis has a closing one.
Show solution (0 XP)
print('Hello') Errors in Python generally fall into two categories. First are Syntax Errors. Syntax errors, also known as parsing errors. This happens before the code is even run, because Python cannot parse what you wrote.
print('Missing parenthesis'SyntaxError
Second are Exceptions. Errors detected during execution. These happen while the code is running, even if the syntax is perfectly valid.
result = 10 / 0
print(result)ZeroDivisionError
When an exception occurs, Python prints a traceback. This shows the context where the exception occurred.
def buggy_function():
return 'string' + 5
buggy_function()TypeError
When you see a traceback, don’t panic! Always look at the bottom first. The last line of the error message indicates what happened.
print(unknown_variable)NameError
The first word on the last line of an error is the exception type. These exception names are built-in identifiers. Some common ones you will encounter include ValueError, TypeError, NameError, and IndexError.
print(int('hello'))ValueError
items = [1, 2, 3]
print(items[5])IndexError
You can watch an exception halt execution and propagate using the visualizer:
try:
def buggy_function():
return 'string' + 5
buggy_function()
except TypeError as e:
print(f"Caught exception: {type(e).__name__}")Are syntax errors and exceptions the exact same thing?
No, syntax errors happen before code executes, while exceptions happen during execution. — Syntax errors happen before code executes because Python can't parse it. Exceptions happen while the code is running.
Where is the most important part of a Python error message located?
At the very bottom of the traceback. — The most important part of a Python error—what actually happened—is located at the very last line of the traceback.
Are tracebacks useless jargon that should be ignored?
No, they show the exact sequence of function calls that led to the error. — Tracebacks show the exact sequence of function calls that led to the error, providing the critical context needed to fix it.
Will errors in Python instantly crash your whole computer?
No, they only crash the running script. — Errors in Python only crash the running script. The sandbox or IDE itself remains completely safe.
The following code has a syntax error. Fix it so it prints 'Hello'.
Make sure every opening parenthesis has a closing one.
print('Hello') This code raises a ZeroDivisionError. Change the divisor from 0 to 2 to fix the bug.
Replace the 0 with a 2.
score = 100
average = score / 2
print(average)