[Tutor] Joining all strings in stringList into one string

Russel Winder russel at winder.org.uk
Wed May 30 19:36:52 CEST 2012


On Wed, 2012-05-30 at 12:21 -0400, Akeria Timothy wrote:
[...]
> def joinStrings(stringList):
>  string = []

indentation error in that the above line and the below line should have
the same indent level.  Also the above line and the following line are
both definitions of the variable string so the above is actually
redundant.

>     for string in stringList:
>         print ''.join(stringList)

Given the variable doesn't appear in the block I wonder if the code
reflects the intended algorithm?

> 
> def main():
>     print joinStrings(['very', 'hot', 'day'])
>     print joinStrings(['this', 'is', 'it'])
>     print joinStrings(['1', '2', '3', '4', '5'])
> 
> main()

The above code, with the trivial indent fix, outputs:

        veryhotday
        veryhotday
        veryhotday
        None
        thisisit
        thisisit
        thisisit
        None
        12345
        12345
        12345
        12345
        12345
        None

Is this what was desired?  I am tempted to think that actually what was
desired was:

        veryhotday
        thisisit
        12345

in which case I would suggest the code should perhaps read:

        def main():
            print ''.join(['very', 'hot', 'day'])
            print ''.join(['this', 'is', 'it'])
            print ''.join(['1', '2', '3', '4', '5'])
        
        if __name__ == '__main__':
            main()

but, mayhap, I am missing the intention.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/tutor/attachments/20120530/962291e1/attachment.pgp>


More information about the Tutor mailing list