[Tutor] '__name__' == '__main__'

Michael Lewis mjolewis at gmail.com
Tue Feb 21 05:55:45 CET 2012

I am back to being confused. I just tried running the module without first
importing it, and it worked just fine. How do I do this properly to where
the module only runs if I import it?


def MultiplyText(text, multiplier):
    '''Recieve a S & int. For digits in S, multiply by multiplier and
return updated S.'''
    return ' '.join(str(int(num) * multiplier) if num.isdigit() else num
for num in text)

def GetUserInput():
    '''Get S & multiplier. Test multiplier.isdigit(). Call
MultiplyText(text, multiplier)'''
    text = raw_input('Enter some text: ')
    while True:
        multiplier = raw_input('Enter a multiplier: ')
            multiplier = int(multiplier)
        except ValueError:
    return MultiplyText(text.split(), multiplier)

if "__name__" == '__main__':

What I did in IDLE:

>>> GetUserInput()
Enter some text: 4 times
Enter a multiplier: 2
'8 times'

Michael J. Lewis

mjolewis at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120220/6e4991c3/attachment-0001.html>

More information about the Tutor mailing list