[Tutor] __name__=='__main__'
Michael Lewis
mjolewis at gmail.com
Tue Feb 21 04:07:09 CET 2012
Now that I am better understanding '__name__'=='__main__', I need to get
advice on one last part. Since you put this in the file as an if statement,
some instruction must come after. What do you suggest putting after this
statement/is that piece of code ever put into action?
In my example below, I've done a few tests like putting a print statement
under '__name__'=='__main__' and it isn't printed. I am not sure what to
put in this code block.
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: ')
try:
multiplier = int(multiplier)
break
except ValueError:
continue
return MultiplyText(text.split(), multiplier)
if '__name__' == '__main__':
GetUserInput()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120220/a1caccbe/attachment.html>
More information about the Tutor
mailing list