0% found this document useful (0 votes)
3 views7 pages

Python - Ala - Code Error

The document provides solutions to a debugging assignment in Python, identifying various errors such as ZeroDivisionError, SyntaxError, and IndentationError, along with their corrected code snippets. Each question outlines the error, presents the corrected code, and shows the expected output. The solutions cover a range of common programming mistakes, demonstrating proper coding practices.

Uploaded by

manthansaraiya82
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Python - Ala - Code Error

The document provides solutions to a debugging assignment in Python, identifying various errors such as ZeroDivisionError, SyntaxError, and IndentationError, along with their corrected code snippets. Each question outlines the error, presents the corrected code, and shows the expected output. The solutions cover a range of common programming mistakes, demonstrating proper coding practices.

Uploaded by

manthansaraiya82
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYTHON PROGRAMMING

MANTHAN SARAIYA
240912030035
ALA 1 Debugging Assignment Solutions

Question 1
Error Identified: ZeroDivisionError. You cannot divide a number by zero. Corrected Code:
Python
a = 5
b = 2 # Changed from 0 to a non-zero number to avoid error
if b != 0:
print(a / b)
else:
print("Cannot divide by zero")
Output:
Plaintext
2.5

Question 2
Error Identified: SyntaxError. Missing colon (:) at the end of the for statement. Corrected
Code:
Python
for i in range(1, 6): # Added colon
print(i)
Output:
Plaintext
1
2
3
4
5

Question 3
Error Identified: IndentationError. The body of the function multiply is not indented.
Corrected Code:
Python
def multiply(a, b):
print(a * b) # Added indentation

multiply(4, 5)
Output:
Plaintext
20

Question 4
Error Identified: SyntaxError. Missing colon (:) after the while condition. Corrected Code:
Python
i = 1
while i <= 10: # Added colon
print(i)
i += 1
Output:
Plaintext
1
2
3
... (prints up to 10)

Question 5
Error Identified: SyntaxError. Missing comma (,) or concatenation operator (+) between the
string "Hello" and the variable name. Corrected Code:
Python
name = input("Enter your name: ")
print("Hello", name) # Added comma
# OR: print("Hello " + name)
Output:
Plaintext
Enter your name: Student
Hello Student

Question 6
Error Identified: SyntaxError. Missing colons (:) after the if and else statements. Corrected
Code:
Python
x = int(input("Enter a number: "))
if x > 0: # Added colon
print("Positive Number")
else: # Added colon
print("Negative Number")
Output:
Plaintext
Enter a number: 10
Positive Number
Question 7
Error Identified: IndexError. List index out of range. The list has indices 0, 1, 2, 3. Index 4
does not exist. Corrected Code:
Python
numbers = [10, 20, 30, 40]
print(numbers[3]) # Changed index to 3 (or any valid index 0-3)
Output:
Plaintext
40

Question 8
Error Identified: Logical Error. The expected output is "Sum of numbers from 1 to 5", but
range(1, 5) stops at 4. Corrected Code:
Python
total = 0
for i in range(1, 6): # Changed range from 5 to 6 to include 5
total = total + i
print("Sum is:", total)
Output:
Plaintext
Sum is: 15

Question 9
Error Identified: TypeError. The function square expects 1 argument (num), but it was called
with none. Corrected Code:
Python
def square(num):
return num * num
print(square(5)) # Passed an argument (e.g., 5)
Output:
Plaintext
25

Question 10
Error Identified: TypeError. The input() function returns a string. You cannot perform
modulo math (%) on a string without converting it to an integer first. Corrected Code:
Python
num = int(input("Enter a number: ")) # Converted input to int
if num % 2 == 0:
print("Even")
else:
print("Odd")
Output:
Plaintext
Enter a number: 4
Even

Question 11
Error Identified: IndentationError. The print statement inside the loop must be indented.
Corrected Code:
Python
for i in range(5):
print("Python Programming") # Added indentation
Output:
Plaintext
Python Programming
Python Programming
Python Programming
Python Programming
Python Programming

Question 12
Error Identified: IndexError. The string "Python" has length 6 (indices 0-5). Index 10 is out of
range. Corrected Code:
Python
text = "Python"
print(text[0]) # Changed to valid index (e.g., 0)
Output:
Plaintext
P

Question 13
Error Identified: TypeError. You cannot add an integer (a) and a string (b) directly. Corrected
Code:
Python
a = 10
b = "20"
print(a + int(b)) # Converted string 'b' to integer
Output:
Plaintext
30
Question 14
Error Identified: The function is referenced but not called. Missing parentheses () in the
function call. Corrected Code:
Python
def display():
print("Welcome to Python")
display() # Added parentheses to call the function
Output:
Plaintext
Welcome to Python

Question 15
Error Identified: KeyError. The key "marks" does not exist in the dictionary student.
Corrected Code:
Python
student = {"name": "Rahul", "age": 20, "marks": 85} # Added 'marks' key
print(student["marks"])
Output:
Plaintext
85

Question 16
Error Identified: SyntaxError. The single equals sign = is for assignment. Comparison
requires double equals ==. Corrected Code:
Python
x = 5
if x == 5: # Changed = to ==
print("Equal")
Output:
Plaintext
Equal

Question 17
Error Identified: ValueError. [Link] cannot calculate the square root of a negative
number in the real number domain. Corrected Code:
Python
import cmath # Use cmath for complex numbers
print([Link](-4))
# OR change the input to a positive number: print([Link](4))
Output:
Plaintext
2j

Question 18
Error Identified: IndentationError or Logical Error. In Python, the statements inside the
while loop must be indented correctly. If num -= 1 is outside the loop, it creates an infinite
loop. Corrected Code:
Python
num = 10
while num > 0:
print(num)
num -= 1 # Indented so it executes inside the loop
Output:
Plaintext
10
9
... (down to 1)

Question 19
Error Identified: SyntaxError. Missing colon (:) after the if condition. Corrected Code:
Python
def find_max(a, b, c):
if a > b and a > c: # Added colon
print(a)
elif b > c:
print(b)
else:
print(c)
find_max(10, 20, 30)
Output:
Plaintext
30

Question 20
Error Identified: Logic/Syntax Error. [Link] is a method and must be called with
parentheses (). Also, it is good practice to ensure the file actually exists before opening.
Corrected Code:
Python
# Assuming [Link] exists
try:
file = open("[Link]", "r")
content = [Link]()
print(content)
[Link]() # Added parentheses
except FileNotFoundError:
print("File not found.")
Output:
Plaintext
(Content of [Link])

You might also like