-1

I'm trying to make a program which prints all the pairs of amicable numbers in a given range (not including by reversing the pair that have already been printed, like if 220, 284 have been printed, 284, 220 cannot be printed). Here's my code below:

def factorgiver(number):
    num=1
    factors=[]
    while num<number:
        if number%num==0:
            factors.append(num)
            num+=1
        else:
            num+=1
    return(factors)
def isamicable(number1, number2):
    q1=factorgiver(number1)
    q2=factorgiver(number2)
    w1=0
    w2=0
    for i in q1:
        w1+=i
    for i in q2:
        w2+=i
    print(w1, w2)
    if w1==number2 and w2==number1:
        return True
    else:
        return False
range1=int(input("Enter the first number of the range: "))
range2=int(input("Enter the last number of the range: "))
range2+=1
for i in range(range1, range2):
    w=i+1
    for q in range(w, range2):
        if isamicable(i, q):
            print(i, q)

I've checked both functions and they are working as intended, but when I run the code, it also includes the numbers lower than my given range and does also gives different output every time.

  • 1
    Could you attach an output to your function? – Eric Jun 28 '21 at 04:44
  • https://stackoverflow.com/questions/38094818/what-is-the-most-efficient-way-to-find-amicable-numbers-in-python does this help –  Jun 28 '21 at 04:50
  • @Eric as i said in the question, the output is not the same, so i dont think it means to add the output – The Blazer Jun 28 '21 at 05:02
  • Even so, it's important to include an example of input and the resulting output. There could be some clues there. – luther Jun 28 '21 at 05:52
  • 1
    From your description of the desired output, it sounds like that `print` call in `isamicable` isn't supposed to be there. – luther Jun 28 '21 at 05:54
  • 1
    It seems to me like luther has the issue identified, you've got a `print` call inside of `isamicable` that prints out the sums of the factors of your two test numbers unconditionally. That's going to be a lot of output, and will almost surely drown out the much rarer output of the actual amicable pairs you get from the outside nested loops. – Blckknght Jun 28 '21 at 21:33
  • @luther Thanks it solved my problem. But still, I can't understand that why the print statement was creating a problem – The Blazer Jun 29 '21 at 02:29
  • It looks like it's because the only output you wanted was the `print` at the bottom. I also don't see anything that could've given you different outputs for the same input. – luther Jun 29 '21 at 12:47

1 Answers1

-1

You're using your variable i in both your root level for-loops and your functions. Try separating your scopes by putting your for-loops into a function as well which you just call from the root level.

Hendrik Wiese
  • 2,010
  • 3
  • 22
  • 49
  • Can you pls elaborate what your'e trying to say ? I mean I can't understnd by what you mesn by root level – The Blazer Jun 28 '21 at 05:46
  • This doesn't seem likely to be the cause of any kind of error. None of the code in the functions is trying to access the global `i`, and the function's local `i` won't modify the global variable either. It's not great style to use very short variable names like `i`, but it's not the reason for any issues that I can see. – Blckknght Jun 28 '21 at 07:03