0% found this document useful (0 votes)
5 views

Python Program

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

Python Program

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
You are on page 1/ 2

Python :

# This program calculates the sum, average, and factorial of all even
numbers from 1 to N (inclusive)

def factorial(n): # Error 1: Function name is misspelled in the call

if n == 0 or n == 1:

return 1

else:

return n * fact(n - 1) # Error 2: Should be factorial, not fact

def calculate_even_numbers_sum_and_average(N):

even_sum = 0

even_count = 0

for i in range(1, N): # Error 3: Should include N, so range(1, N+1)

if i % 2 == 0:

even_sum += i

even_count += 1

avg = even_sum // even_count # Error 4: Integer division used instead of


float division (/)

return even_sum, avg

def main():

N = input("Enter a number: ") # Error 5: Input not converted to int

if N < 1: # Error 6: N is still a string, comparison will fail

print("Please enter a positive integer greater than 1.")

return

even_sum, even_avg = calculate_even_numbers_sum_and_average(N) #


Error 7: N is passed as string, not int

print("The sum of even numbers from 1 to", N, "is", even_sum)

print("The average of even numbers is", even_avg)


even_numbers = [i for i in range(1, N) if i % 2 == 0] # Error 8: N should be
N+1

factorials = []

for num in even_numbers:

fact_result = fact(num) # Error 9: fact is not defined, should be factorial

factorials.append(fact_result)

print("Factorials of even numbers are:")

for i in range(0, even_numbers): # Error 10: range should be


len(even_numbers), not even_numbers itself

print("Factorial of", even_numbers[i], "is", factorials[i]) # Error 11: Incorrect


index usage in loop

main()

def calculate_average(sum, count): # Error 12: This function is never called


or used

if count == 0:

return 0

return sum / count

You might also like