user216485
user216485

Reputation: 789

Python return statement not running

I am creating a program to figure out the highest number of decimals in a list of numbers. Basically, a list with [123, 1233] would return 4 because 1233 has four numbers in it and it is the largest. Another example would be that [12, 4333, 5, 555555] would return 6 because 555555 has 6 numbers.

Here is my code.

def place(listy):  
    if len(listy) == 1:  
        decimal = len(str(listy[0]))    
        print(decimal)  
    else:  
        if len(str(listy[0])) >= len(str(listy[1])):  
            new_list = listy[0:1]  
            for i in listy[2:]:  
                new_list.append(i)  
            place(new_list)  
        else:   
            place(listy[1:]) 

Now, when I use print(decimal) it works, but if I change print(decimal) to return decimal, it doesn't return anything. Why is this? How do I fix this? I have come across these return statements which doing run a lot of times. Thanks in advance!

Upvotes: 1

Views: 8697

Answers (5)

Krissh
Krissh

Reputation: 89

The function does return the value, but it's not printing it out.
A simple way to solve this is, just call the function within a print statement.
That is:

print(place(listy))

Upvotes: 1

Zillionz
Zillionz

Reputation: 1

Use ''global variable'' (google it) to access and change a variable defined outside of your function.

Upvotes: -2

uselpa
uselpa

Reputation: 18917

When you do a recursive call (i.e. when place calls place, and the called place returns a value, then the calling place must return it as well (i.e. the return value "bubbles up" to the initial caller).

So you need to replace every recursive call

place(...)

with

return place(...)

As others have said, there are easier solutions, such as using max(). If you want to keep a recursive approach, I would refactor your code as follows:

def place2(listy):
    if len(listy) < 1:
        return None
    elif len(listy) == 1:
        return len(str(listy[0]))
    else:
        v0, v1 = listy[0], listy[1]
        if v1 > v0:
            return place2(listy[1:])
        else:
            return place2([listy[0]]+listy[2:])

Although this is tail-recursive, Python does not really care so this approach will be inefficient. Using max(), or using a loop will be the better solution in Python.

Upvotes: 9

jedwards
jedwards

Reputation: 30210

If all you want is to find the maximum length of a list of integers, consider:

max([len(str(n)) for n in N])

For example

N = [1,22,333,4444]
max([len(str(n)) for n in N]) # Returns 4

N = [12, 4333, 5, 555555]
max([len(str(n)) for n in N]) # Returns 6

Note: This will only work for positive integers.

Or more simply:

len(str(max(N)))

Which will also only work for positive integers.

Upvotes: 0

FatalError
FatalError

Reputation: 54551

It's not that the return doesn't do anything, it's that you don't propagate the return from your recursive call. You need a few more returns:

def place(listy):  
    if len(listy) == 1:  
        decimal = len(str(listy[0]))    
        return decimal
    else:  
        if len(str(listy[0])) >= len(str(listy[1])):  
            new_list = listy[0:1]  
            for i in listy[2:]:  
                new_list.append(i)  
            return place(new_list)  # <-- return added
        else:   
            return place(listy[1:]) # <-- return added

You can see the print at any level, but to get it back to the caller it needs to be propagated.

Upvotes: 1

Related Questions