Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

CodeFix Python Programs

Uploaded by

samabishek75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CodeFix Python Programs

Uploaded by

samabishek75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

AVERAGE OF A LIST:
Correct Version :

# This code correctly finds the average of a list of numbers


def calculate_average(numbers):
total = sum(numbers)
average = total / len(numbers)
print("The average is", average)

numbers = [10, 20, 30, 40]


calculate_average(numbers)
output:
The average is 25.0

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

numbers = [10, 20, 30, 40]


calculate_average(number) # Error: 'number' is undefined, should be 'numbers'

2. FACTORIAL 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

3. AVERAGE OF POSITIVE NUMBER IN A LIST


Correct Version:
# This program calculates the average of positive numbers in a list

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

numbers = [-5, -10, -15]


print("The average of positive numbers is", average_positive_numbers(numbers))
output:
The average of positive numbers is No positive numbers in the list

error version:

# This program is supposed to calculate the average of positive numbers in a list

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

numbers = [-5, -10, -15]


print("The average of positive numbers is", average_positive_numbers(numbers)) # Error: Function
might return None

4. FIND THE MAXIMUM NUMBER IN A LIST:


Correct Version:

# 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 = [] # Error: Empty list will cause an IndexError


print("The maximum number is:", find_maximum(numbers))

5. SUM OF EVEN NUMBER IN A LIST


Correct Version:
# This program calculates the sum of even numbers in a list
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

6. CHECK IF A GIVEN NUMBER IS PRIME:


Correct 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'}")
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'}")

7. FIBONACCI SEQUENCE UP TO A SPECIFIED NUMBER


Correct 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
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

def calculate_area(length, width):


if length < 0 or width < 0:
return "Error: Length and width must be positive numbers."
area = length * width
return area

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

def calculate_area(length, width):


if length < 0 or width < 0: # Error: Missing return statement for invalid inputs
print("Length and width must be positive numbers.");
area = length * width # Error: This line will still execute even if inputs are invalid
return area;

length = -5
width = 10
print("The area of the rectangle is:", calculate_area(length, width))

9. REVERSE A STRING AND CHECK IF IT'S A PALINDROME:


Correct Version:
# This program reverses a string and checks if it is a palindrome

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

10. SQUARE ROOT OF A NUMBER:


Correct Version:
# This program calculates the square root of a number, with error handling for negative inputs

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

***ALL THE BEST***


1. AVERAGE OF A LIST:
Correct Version :
# This code correctly finds the average of a list of numbers
def calculate_average(numbers):
total = sum(numbers)
average = total / len(numbers)
print("The average is", average)

numbers = [10, 20, 30, 40]


calculate_average(numbers)
output:
The average is 25.0
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

numbers = [10, 20, 30, 40]


calculate_average(number) # Error: 'number' is undefined, should be 'numbers'

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

3. AVERAGE OF POSITIVE NUMBER IN A LIST


Correct Version:
# This program calculates the average of positive numbers in a list

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

numbers = [-5, -10, -15]


print("The average of positive numbers is", average_positive_numbers(numbers))
output:
The average of positive numbers is No positive numbers in the list
error version:
# This program is supposed to calculate the average of positive numbers in a list
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

numbers = [-5, -10, -15]


print("The average of positive numbers is", average_positive_numbers(numbers)) # Error: Function
might return None

4. FIND THE MAXIMUM NUMBER IN A LIST:


Correct Version:
# 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 = [] # Error: Empty list will cause an IndexError


print("The maximum number is:", find_maximum(numbers))
5. SUM OF EVEN NUMBER IN A LIST
Correct Version:
# This program calculates the sum of even numbers in a list

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

6. CHECK IF A GIVEN NUMBER IS PRIME:


Correct 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'}")
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'}")

7. FIBONACCI SEQUENCE UP TO A SPECIFIED NUMBER


Correct 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
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

def calculate_area(length, width):


if length < 0 or width < 0:
return "Error: Length and width must be positive numbers."
area = length * width
return area

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

def calculate_area(length, width):


if length < 0 or width < 0: # Error: Missing return statement for invalid inputs
print("Length and width must be positive numbers.");
area = length * width # Error: This line will still execute even if inputs are invalid
return area;

length = -5
width = 10
print("The area of the rectangle is:", calculate_area(length, width))

9. REVERSE A STRING AND CHECK IF IT'S A PALINDROME:


Correct Version:
# This program reverses a string and checks if it is a palindrome

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

10. SQUARE ROOT OF A NUMBER:


Correct Version:
# This program calculates the square root of a number, with error handling for negative inputs

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

***ALL THE BEST***

You might also like