问题
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?
回答1:
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
回答2:
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.
回答3:
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))
来源:https://stackoverflow.com/questions/16103187/function-doesnt-return-all-results-from-for-loop