CodeFix Python Programs
CodeFix Python Programs
AVERAGE OF A LIST:
Correct Version :
error version:
# This code is meant to find the average of a list of numbers
def calculate_average(numbers):
total = sum(numbers)
average = total / len # Error: len is not used correctly
print("The average is " average) # Error: Missing comma between strings and variables
2. FACTORIAL OF A NUMBER:
Correct Version:
def factorial(n):
if n < 0:
return "Factorial is not defined for negative numbers"
elif n == 0:
return 1
else:
return n * factorial(n - 1)
number = 5
print("The factorial of", number, "is", factorial(number))
output:
The factorial of 5 is 120
error version:
# This program calculates the factorial of a number
def factorial(n):
if n == 0
return 1 # Error: Missing colon in the if statement
else:
return n * factorial(n - 1) # Error: Incorrect recursion logic for negative numbers
number = 5
print("The factorial of", number, "is", factorial(number)) # Error: Missed edge case when number < 0
def average_positive_numbers(numbers):
total = 0
count = 0
for num in numbers:
if num > 0:
total += num
count += 1
if count == 0:
return "No positive numbers in the list"
else:
average = total / count
return average
error version:
def average_positive_numbers(numbers):
total = 0
count = 0
for num in numbers:
if num > 0:
total += num
count += 1
average = total / count # Error: Division by zero if count is zero
return average # Error: Average is not calculated when there are no positive numbers
# This program finds the maximum number in a list, handling empty lists
def find_maximum(numbers):
if not numbers: # Check if the list is empty
return "List is empty, no maximum number."
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
numbers = []
print("The maximum number is:", find_maximum(numbers))
output:
The maximum number is: List is empty, no maximum number.
error version:
# This program is supposed to find the maximum number in a list
def find_maximum(numbers):
max_num = numbers[0] # Error: Assumes list is non-empty
for num in numbers:
if num > maxmum: # Error: Typo in 'max_num' variable name
max_num = num
return max_num
numbers = [1, 2, 3, 4, 5]
total = sum_even_numbers(numbers) # Store the function result
print("The sum of even numbers is:",total)
output:
The sum of even numbers is: 6
error version:
# This program is supposed to calculate the sum of even numbers in a list
def sum_even_numbers(numbers):
total = 0
for num in numbers:
if num % 2 = 0: # Error: Assignment operator used instead of equality check
total += num
return total
numbers = [1, 2, 3, 4, 5]
print("The sum of even numbers is:" total) # Error: Missing comma in the print statement
def is_prime(num):
if num <= 1:
return False
for i in range(2, num // 2 + 1): # Fixed range with integer division
if num % i == 0:
return False
return True
number = 15
print(f"Is {number} a prime number? {'Yes' if is_prime(number)else'No'}")
output:
Is 15 a prime number? No
error version:
# This program checks if a given number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, num // 2 + 1): # Fixed range with integer division
if num % i == 0:
return False
return True;
number = 15:
print(f"Is {number} a prime number? {'Yes' if is_prime(number) else 'No'}")
def fibonacci(n):
if n <= 0:
return [] # Handle case for non-positive n
elif n == 1:
return [0] # Handle case for n = 1
fib_sequence = [0, 1]
for i in range(2, n): # Loop from 2 to n (exclusive)
fib_sequence.append(fib_sequence[i - 1] + fib_sequence[i - 2])
return fib_sequence
n=5
print("Fibonacci sequence up to", n, "is:", fibonacci(n)) # Improved output format
output:
Fibonacci sequence up to 5 is: [0, 1, 1, 2, 3]
error version:
# This program calculates the Fibonacci sequence up to a specified number
def fibonacci(n)
if n <= 0:
return []; # Handle case for non-positive n
elif n == 1:
return [0] # Handle case for n = 1
fib_sequence = [0, 1]:
for i in range(2, n): # Loop from 2 to n (exclusive)
fib_sequence.append(fib_sequence[i - 1] + fib_sequence[i - 2])
return fib_sequence
n=5
print("Fibonacci sequence up to", n, "is:", fibonacci(n)) # Improved output format
8. FIND THE AREA OF RECTANGLE:
Correct Version:
# This program calculates the area of a rectangle with error handling for invalid inputs
length = 5
width = 10
area = calculate_area(length, width)
if isinstance(area, str): # Check if the returned value is an error message
print(area)
else:
print("The area of the rectangle is:", area)
output:
The area of the rectangle is: 50
error version:
# This program is supposed to find the area of a rectangle
length = -5
width = 10
print("The area of the rectangle is:", calculate_area(length, width))
def is_palindrome(s):
reversed_s = s[::-1] # Corrected to use slicing for reversing the string
if s == reversed_s:
return True
else:
return False
input_string = "madam"
print(f"Is '{input_string}' a palindrome? {'Yes' if is_palindrome(input_string) else 'No'}")
output:
Is 'madam' a palindrome? Yes
error version:
# This program is meant to reverse a string and check if it's a palindrome
def is_palindrome(s):
reversed_s = s.reverse() # Error: .reverse() modifies lists in place; does not work on strings
if s == reversed_s:
return True
else:
return Flase # Error: Typo in "False"
input_string = "madam"
print("Is the string a palindrome?", is_palindrome(input_string)) # Error: Poor output message clarity
import math
def calculate_square_root(num):
if num < 0:
return "Error: Cannot calculate the square root of a negative number."
result = math.sqrt(num)
return result
number =9
result = calculate_square_root(number)
if isinstance(result, str): # Check if the result is an error message
print(result)
else:
print("The square root of the number is:", result)
output:
The square root of the number is: 3.0
error version:
# This program is supposed to calculate the square root of a number
import math
def calculate_square_root(num):
if num < 0:
print("Cannot calculate the square root of a negative number") # Error: Should return or handle
this case properly
result = math.sqrt(num) # Error: This line will still execute even if num is negative
return result
number = -9
print("The square root of the number is:", calculate_square_root(number))
2. FACKTORIAL OF A NUMBER:
Correct Version:
# This program calculates the factorial of a number
def factorial(n):
if n < 0:
return "Factorial is not defined for negative numbers"
elif n == 0:
return 1
else:
return n * factorial(n - 1)
number = 5
print("The factorial of", number, "is", factorial(number))
output:
The factorial of 5 is 120
error version:
# This program calculates the factorial of a number
def factorial(n):
if n == 0
return 1 # Error: Missing colon in the if statement
else:
return n * factorial(n - 1) # Error: Incorrect recursion logic for negative numbers
number = 5
print("The factorial of", number, "is", factorial(number)) # Error: Missed edge case when number < 0
def average_positive_numbers(numbers):
total = 0
count = 0
for num in numbers:
if num > 0:
total += num
count += 1
if count == 0:
return "No positive numbers in the list"
else:
average = total / count
return average
def find_maximum(numbers):
if not numbers: # Check if the list is empty
return "List is empty, no maximum number."
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
numbers = []
print("The maximum number is:", find_maximum(numbers))
output:
The maximum number is: List is empty, no maximum number.
error version:
# This program is supposed to find the maximum number in a list
def find_maximum(numbers):
max_num = numbers[0] # Error: Assumes list is non-empty
for num in numbers:
if num > maxmum: # Error: Typo in 'max_num' variable name
max_num = num
return max_num
def sum_even_numbers(numbers):
total = 0
for num in numbers:
if num % 2 == 0: # Corrected to equality check
total += num
return total
numbers = [1, 2, 3, 4, 5]
total = sum_even_numbers(numbers) # Store the function result
print("The sum of even numbers is:",total)
output:
The sum of even numbers is: 6
error version:
# This program is supposed to calculate the sum of even numbers in a list
def sum_even_numbers(numbers):
total = 0
for num in numbers:
if num % 2 = 0: # Error: Assignment operator used instead of equality check
total += num
return total
numbers = [1, 2, 3, 4, 5]
print("The sum of even numbers is:" total) # Error: Missing comma in the print statement
def is_prime(num):
if num <= 1:
return False
for i in range(2, num // 2 + 1): # Fixed range with integer division
if num % i == 0:
return False
return True
number = 15
print(f"Is {number} a prime number? {'Yes' if is_prime(number)else'No'}")
output:
Is 15 a prime number? No
error version:
# This program checks if a given number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, num // 2 + 1): # Fixed range with integer division
if num % i == 0:
return False
return True;
number = 15:
print(f"Is {number} a prime number? {'Yes' if is_prime(number) else 'No'}")
def fibonacci(n):
if n <= 0:
return [] # Handle case for non-positive n
elif n == 1:
return [0] # Handle case for n = 1
fib_sequence = [0, 1]
for i in range(2, n): # Loop from 2 to n (exclusive)
fib_sequence.append(fib_sequence[i - 1] + fib_sequence[i - 2])
return fib_sequence
n=5
print("Fibonacci sequence up to", n, "is:", fibonacci(n)) # Improved output format
output:
Fibonacci sequence up to 5 is: [0, 1, 1, 2, 3]
error version:
# This program calculates the Fibonacci sequence up to a specified number
def fibonacci(n)
if n <= 0:
return []; # Handle case for non-positive n
elif n == 1:
return [0] # Handle case for n = 1
fib_sequence = [0, 1]:
for i in range(2, n): # Loop from 2 to n (exclusive)
fib_sequence.append(fib_sequence[i - 1] + fib_sequence[i - 2])
return fib_sequence
n=5
print("Fibonacci sequence up to", n, "is:", fibonacci(n)) # Improved output format
8. FIND THE AREA OF RECTANGLE:
Correct Version:
# This program calculates the area of a rectangle with error handling for invalid inputs
length = 5
width = 10
area = calculate_area(length, width)
if isinstance(area, str): # Check if the returned value is an error message
print(area)
else:
print("The area of the rectangle is:", area)
output:
The area of the rectangle is: 50
error version:
# This program is supposed to find the area of a rectangle
length = -5
width = 10
print("The area of the rectangle is:", calculate_area(length, width))
def is_palindrome(s):
reversed_s = s[::-1] # Corrected to use slicing for reversing the string
if s == reversed_s:
return True
else:
return False
input_string = "madam"
print(f"Is '{input_string}' a palindrome? {'Yes' if is_palindrome(input_string) else 'No'}")
output:
Is 'madam' a palindrome? Yes
error version:
# This program is meant to reverse a string and check if it's a palindrome
def is_palindrome(s):
reversed_s = s.reverse() # Error: .reverse() modifies lists in place; does not work on strings
if s == reversed_s:
return True
else:
return Flase # Error: Typo in "False"
input_string = "madam"
print("Is the string a palindrome?", is_palindrome(input_string)) # Error: Poor output message clarity
import math
def calculate_square_root(num):
if num < 0:
return "Error: Cannot calculate the square root of a negative number."
result = math.sqrt(num)
return result
number =9
result = calculate_square_root(number)
if isinstance(result, str): # Check if the result is an error message
print(result)
else:
print("The square root of the number is:", result)
output:
The square root of the number is: 3.0
error version:
# This program is supposed to calculate the square root of a number
import math
def calculate_square_root(num):
if num < 0:
print("Cannot calculate the square root of a negative number") # Error: Should return or handle
this case properly
result = math.sqrt(num) # Error: This line will still execute even if num is negative
return result
number = -9
print("The square root of the number is:", calculate_square_root(number))