Python1
Python1
Objective: Identify, correct, and explain errors in Python code snippets to reinforce
understanding of syntax and logic.
Code Snippets
1.**Code Snippet 1**
Error: def add_numbers(a, b)
SyntaxError: invalid syntax
Corrected Code:
def add_numbers(a, b):
return a + b
print(add_numbers(5, 10))
Explanation: In Python, the colon (:) tells the interpreter that a block of code is about to start
— like the body of a function, loop, or if statement. Without it, Python doesn't know where the
function body begins, so it raises a SyntaxError.
2. **Code Snippet 2**
Error: name ="Alice
unterminated string literal (detected at line 1)
Corrected Code:
name ="Alice"
print("Hello, " + name)
Explanation: Missing the closing double quote at the end of the string "Alice". Python starts
reading a string when it sees the first ", but because it doesn't find a second " to close it, it
throws a SyntaxError.
3. **Code Snippet 3**
Error: for i in range(5)
SyntaxError: expected ':'
Correct Code:
for i in range(5):
print("Number:",i)
Explanation: In Python, all control flow statements (for, if, while, etc.) must end with a colon
to indicate the start of a block of code.
4. **Code Snippet 4**
Error: print("The fifth element is: " + my_list[5])
IndexError: list index out of range
Correct Code:
my_list = [1,2,3,4,5]
print("The fifth element is: " + str(my_list[4]))
Explanation:Index 5 doesn’t exist — the list has only 5 elements (indices 0 to 4) and use str()
to convert the number to a string so it can be joined with the text.
5. **Code Snippet 5**
Error: Expected an indented block after function definition on line 3
def greet(name):
print("Hello " + name)
greet("Bob")
Correct Code:
def greet(name):
print("Hello " + name)
greet("Bob")
Explanation: In Python, indentation tells the interpreter what code belongs to a function, loop,
or conditional block. After defining a function with def, everything that should be part of the
function must be indented.
6. **Code Snippet 6**
Error: age = input("Enter your age: ")
if age>=18:
TypeError: '>=' not supported between instances of 'str' and 'int'.
Correct Code:
age = int(input("Enter your age: "))
if age>=18:
print("you are eligible to vote.")
else:
print("you are not eligible to vote. ")
Explanation: When we use input(), Python treats whatever the user types as text, not a number.
So,we need to convert the text to a number using int(),then it's dealing with a number, and the
comparison works.
7. **Code Snippet 7**
Error: 'return' outside function
def multiply(a,b):
result = a*b
return result
Correct Code:
def multiply(a, b):
result = a * b
return result
print(multiply(4, 5))
Explanation: In Python, everything that belongs inside a function must be indented, return
result is part of the function because it's indented correctly.
8. **Code Snippet 8**
Error: while count > 0
Correct Code:
count = 10
while count > 0:
print(count)
count -= 1
print("countdown complete!")
Explanation: In Python, the colon (:) tells the interpreter that a block of code is about to start
— like the body of a function, loop, or if statement. Without it, Python doesn't know where the
function body begins, so it raises a SyntaxError.