[SOLVED] Getting False with my Def, What is wrong with my code?

Issue

This Content is from Stack Overflow. Question asked by Minh

    def richNumber(n):
        nb = []
        n = int(n)
        sum1 = 0
        for i in(n, n+1):
            if n % i ==0:
                nb.append(i)
        sum1 = sum(nb) - nb[-1]
        if sum1 > n:
            return True
        else:
            return False
    
    n = int(input("n:"))

print(richNumber(n))

I have 5 Test Cases:
n = 4
n = 12
n = 6
n = 20
n = 100

With n = 4 and 6 the output is false
,with n = 12,20,100 is supposed to be true but its showing false



Solution

for i in (n, n+1)

Iterates over two numbers, n and n + 1, not all divisors. You need to use range to iterate from 1 to n

for i in range(1, n + 1)


This Question was asked in StackOverflow by Minh and Answered by Guy It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?