Ask Question

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself). Write a Python3 program to check if a user-entered number is a perfect number or not. Use exception handling to handle invalid inputs.

+1
Answers (1)
  1. 13 August, 18:22
    0
    def the_perfect (n):

    try: #exception handling if n is a negative number

    n > 0

    except: #return - 1 if the error is reached

    return - 1

    else:

    total = 0

    for i in range (1, n) : #for loop from 1 to the target number

    if n % i = = 0:

    total + = i

    return total = = n #should return true if number is perfect number

    print (perfect_number (8))
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers