Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function doesn't return all results from 'for' loop

I've made a simple function to print out a times table chart depending on the number you decide to run with. The problem I'm having due to my basic understanding of the language is why it only returns the first loop and nothing else.

def timestables(number):
  for a in range(1, number+1):
    b = a*a
    c = a
    return (str(c) + " * " + str(c) + " = " + str(b))

print(timestables(5))

I get the answer ..

1 * 1 = 1

I've tried to rectify this issue by using print instead of return but this ultimately results with a None appearing as well.

def timestables(number):
  for a in range(1, number+1):
    b = a*a
    c = a
    print (str(c) + " * " + str(c) + " = " + str(b))

print(timestables(5))

I get the answer ..

1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
4 * 4 = 16
5 * 5 = 25
None

How can I return all given results from the for loop to avoid a None error?

like image 709
Ergo Avatar asked Nov 28 '22 02:11

Ergo


2 Answers

yield them.

def timestables(number):
  for a in range(1, number+1):
    yield '%s + %s = %s' % (a, a, a*a )

for x in timestables(5):
  print x

This turns your function into a generator function, and you need to iterate over the results, i.e. the result is not a list, but an iterable.

If you need a list, the simplest is to explicitly create one:

res = list(timestables(5))

But again, if you don't, you don't.

IMHO, this is the most pythonic way.

like image 178
shx2 Avatar answered Dec 05 '22 16:12

shx2


You're returning inside the for loop - and functions stop execution immediately once they hit a return statement.

To work around this, you can use a list to store those values, and then return that list.

def timestables(number):
    lst = []
    for a in range(1, number+1):
        b = a*a
        c = a
        lst.append(str(c) + " * " + str(c) + " = " + str(b))
    return lst

As a side note, you should use string formatting to build the string, like so.

lst.append('{a} * {a} = {b}'.format(a=a, b=a*a))

Now we can get rid of all those intermediate variables (b and c), and we can use a list comprehension instead.

def timestables(number):
    return ['{a} * {a} = {b}'.format(a=a, b=a*a) for a in range(1, number+1)]

If you don't want the function to return a list, but a multi-line string, you can use str.join:

def timestables(number):
    return '\n'.join('{a} * {a} = {b}'.format(a=a, b=a*a) for a in range(1, number+1))

Now we can test the function:

>>> print(timestables(5))
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
4 * 4 = 16
5 * 5 = 25
like image 38
Volatility Avatar answered Dec 05 '22 18:12

Volatility