1

I am new to programming and I tried some of the easy leet code problems. When I run the code on my own it returns the correct boolean value but when I run in leetcode it returns the opposite. Like it returns True in vs code while it returns false in Leetcode. It is a simple program to check if it is a perfect number or not.

class Solution:
    def checkPerfectNumber(self, num: int) -> bool:
        num = int(input("Enter number: "))
        sum = 0
    
        for i in range(1,num):
            if num % i == 0:
                sum = sum + i

        if sum == num:
            return True
        else:
            return False
Robert
  • 7,394
  • 40
  • 45
  • 64
Newbie100
  • 11
  • 3
  • Also, remember that it is `True/False`, not `true/false`. Did you retype this? – Tim Roberts May 20 '23 at 05:12
  • You can simplify your code by just doing `return num == sum` instead of your `if num == sum` And without knowing the specifications of your problem: are you sure you are expected to get the `input` within the function and not use the parameter provided to the function? – derpirscher May 20 '23 at 07:56

1 Answers1

1

You are not expected to read a value using input. The correct value is passed in to the function in the num parameter, which you throw away. If you want to TEST this, add a line at the end like

print(Solution().checkPerfectNumber(int(input("Enter number:"))))
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30