[Tutor] Evaluate my script?
Kent Johnson
kent37 at tds.net
Tue Oct 24 19:53:51 CEST 2006
Adam Gomaa wrote:
> Thanks for your help! I've rewritten it with many of your suggestions
> and will definitely use this as a reference for future scripts.
>
> Here's the updated one.
> userInputString=userInput.strip(string.digits)
> userInputString=userInputString.strip(string.punctuation)# this one
> userInputString=userInputString.strip(string.digits)
> userInputString=userInputString.strip(string.whitespace)
> userInputString=userInputString.strip(string.digits)# and this one.
>
>
> userInputValue=userInput.strip(string.letters)
> userInputValue=userInputValue.strip(string.whitespace)
I'm not entirely sure I follow this but I think you could use
userInputString=userInput.strip(string.digits+string.punctuation+string.digits+string.whitespace)
userInputValue=userInput.strip(string.letters+string.whitespace)
> def convertUnits(startingUnit,value):
> """Convert the passed value, with startingUnit, into appropriate units
> for the rest of the module."""
>
> #For next if block, each of these should pass value into a lambda
> # function from the dictionaries above. In theory, then, it should
> # always convert into kPa, l, or k.
> if startingUnit in pressureUnitDict.keys(): #WHY the error?
> return pressureUnitDict[startingUnit](value)
>
> elif startingUnit in volumeUnitDict.keys():
> return volumeUnitDict[startingUnit](value)
>
> elif startingUnit in temperatureUnitDict.keys():
> return temperatureUnitDict[startingUnit](value)
>
> elif startingUnit=='x':
> return value
> else:
> print '### Unknown unit "%s" -- Using default. ###' % startingUnit
> return value
This is ugly. I would either
- pass the correct dict as an argument
- make separate convertPressure(), convertVolume(), convertTemperature()
functions, which probably will pass the correct dict to a helper function.
- Just put all the conversions in one dict, which will give the same
functionality you have now.
Kent
More information about the Tutor
mailing list