On 02/20/2012 10:07 PM, Michael Lewis wrote:
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()


I don't see any print statement in the if-clause. In fact I don't see any prints in the entire program. Just how do you expect to know if it even ran?

What IS there is a call to GetUserInput(). But for some reason that function doesn't return user-input. Why not? A function should nearly always take some arguments, and return a result. And its name should reflect what it's going to do

Besides that, you do return a value from GetUserInput()., but never use that value. So what's the point?

Probably what belongs in the if clause is a call to main(). Then you need to write main() function, to call GetUserInput() and save the value returned (a tuple of string and int, my choice). Then it'd call MultiplyText with those two, and with this mysterious num value that you conjured up in MultiplyText.

Once you've factored the two functions into separately called entities, you have a chance of debugging your multiple mistakes still remaining. In between the two function calls in main(), you can actually print out your intermediate results, and see if they look reasonable to you.


In fact, while testing, you just might want to try calling MultiplyText() with whatever literal arguments make sense. And print the result you get. A few of those tests, and you might get comfortable with the function.

--

DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to