[Solved] Why is my code displaying the wrong output?


Sorry for the confusion regarding my previous answer. The problem was that you were appending n instead of i in myList.append(n). Moreover, you could simply use sum to sum your list.

Your output was wrong because you were appending the number n and hence when you do sum=sum+myList[i], you were just adding n to the sum three times because instead of adding 1, 2, 3 to sum, you were adding 6, 6, 6.

n=int(input())
myList=[]

for i in range(1,n):
    if n%i==0:
        myList.append(i)

if sum(myList)==n:
    print("Yes")
else:
    print("No")

A one liner suggested by Matthias

print(('No', 'Yes')[sum(i for i in range(1, n) if not n % i) == n])

4

solved Why is my code displaying the wrong output?