Can someone tell me why i get None at the end please this has me stuck for ages
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Feb 23 19:35:14 EST 2009
En Mon, 23 Feb 2009 17:22:16 -0200, Gary Wood <woodygar at sky.com> escribió:
> '''exercise to complete and test this function'''
> import string
> def joinStrings(items):
> '''Join all the strings in stringList into one string,
> and return the result. For example:
> >>> print joinStrings(['very', 'hot', 'day'])
> 'veryhotday'
> '''
> word = [items]
> for item in items:
> print (item, end='')
> def main():
> print(joinStrings(['very', 'hot','day']))
> print(joinStrings(['this', 'is','it']))
> print(joinStrings(['1', '2', '3', '4', '5']))
>
> main()
See this:
py> def sum(a, b):
... "Returns the sum of two numbers"
... print a+b
...
py> print sum(2,3)
5
None
The function above does *not* do what is expected. It should compute a+b
AND RETURN THE RESULT (instead, it PRINTS the result and returns nothing,
None in Python parlance). It's up to the caller to decide what to do with
the resultant value; if he wants to print it, fine; but maybe he wants to
carry on other operations:
py> print sum(2,3) * 4
5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
If the function claims to *return* something, it should return that thing:
py> def sum(a, b):
... "Returns the sum of two numbers"
... return a+b
...
py> print sum(2,3)
5
py> print sum(2,3) * 4
20
Back to your exercise, you have to join the strings together WITHOUT USING
PRINT. You have to build a NEW string, joining the pieces one by one, and
return the new, concatenated string. If I'm not mistaken, there were some
clues in the exercise statement that you're ignoring and should help a lot.
--
Gabriel Genellina
More information about the Python-list
mailing list