5

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?

shx2
  • 61,779
  • 13
  • 130
  • 153
Ergo
  • 393
  • 1
  • 3
  • 9

3 Answers3

16

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.

shx2
  • 61,779
  • 13
  • 130
  • 153
11

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
Volatility
  • 31,232
  • 10
  • 80
  • 89
  • 1
    Would be better with `yield` or a generator expression than building a list, but +1 for explaining *why* `return` doesn't do what the OP expects. – lvc Apr 19 '13 at 11:53
  • Thanks for your detailed response, it looks much cleaner!! I'm trying to figure out how to get this desired result by only using a `print` function however. – Ergo Apr 19 '13 at 13:21
0

You can return an array:

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

    return out
print(timestables(5))
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162