0

Possible Duplicate:
Why does 'return self' return None?

I've been trying to solve Problem 55 on Project Euler (http://projecteuler.net/problem=55) and now that I think I have the answer, I Experience a problem. I don't want a solution to the Problem 55, just on what I've done wrong.

Here's my code: (I don't think you need all of it)

t=0
lychrel=0
called=0

def iteratepal(n):
    global t
    global called
    called+=1
    b = int(''.join(reversed(str(n))))
    #print ("n =",n,"\nb =",b,"\nb+n =",b+n,"\n")

    if ispal(b+n) or ispal(n):
        t=0
        return False

    if t<50:
        t+=1
        iteratepal(b+n)
    else:                          # Here's the prob
        t=0                        # this block is executed (because it prints "yea")
        print("yea")               # but it doesn't return True!
        return True                # you can try it yourself (even in the interpreter)

def ispal(n):
    if n == int(''.join(reversed(str(n)))):
        return True
    return False

print(iteratepal(196))

for i in range(0,200):
    if iteratepal(i)==True:
        lychrel+=1
        print(i,"is Lychrel!")
    else:
        print(i,"is not a Lychrel!")
print(lychrel)

Thanks for any help, I'm really confused with this.

Community
  • 1
  • 1

2 Answers2

5

You call the function recursively when t < 50, but don't do anything with the return value:

if t<50:
    t+=1
    iteratepal(b+n)
else:                          
    t=0                        
    print("yea")               
    return True

The else: branch is never executed then, so None is returned instead. You probably want to return the result of the recursive call:

if t<50:
    t+=1
    return iteratepal(b+n)
else:                          
    t=0                        
    print("yea")               
    return True

Some further tips:

  • There is no need to test for ==True in an if statement, the following will work just fine:

    if iteratepal(i):
    
  • You can return the test in def ispal(n) is itself a boolean result, just return that without testing:

    def ispal(n):
        return n == int(''.join(reversed(str(n))))
    
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • thank you that worked, although i don't really understand the point of return , in recursion . I mean, you just call a function with new data, why whould you need to return that? – Makis Renieris Sep 21 '12 at 21:15
  • thanks for the further tips too! – Makis Renieris Sep 21 '12 at 21:26
  • @MakisRenieris, if you don't return it then it isn't recursive, it get's called once and never gets returned to the original calling function. Not only that each iteration of the recursion doesn't gets returned to the level above, which means no result ever gets returned. – Ben Sep 21 '12 at 21:27
0

You aren't returning the result of the recursion. Change this

if t<50:
    t+=1
    iteratepal(b+n)

to this

if t<50:
    t+=1
    return iteratepal(b+n)
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214