osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How call method from a method in same class?


On 4/1/19 10:02 PM, Dave wrote:
> As classes get more complex, it is good to call a function to do some of 
> the processing, and make the code easier to follow.? My question is how 
> to do that?? I've attached some silly code to illustrate the point.? The 
> error is: name 'validScale' is not defined.? Well, yes it is, but maybe 
> not the correct way.? Suggestions?
> 
> Dave,
> 
> class TempConverter():
>  ??? """ Temperature Converter converts a tempeature from one scale
>  ??????? to another scale.? For example: 32, F, C will return
>  ??????? 0 degrees C
>  ??? """
> 
>  ??? def __init__(self, temperature, scale, newScale):
>  ??????? self.temperature = temperature
>  ??????? self.scale = scale
>  ??????? self.newScale = newScale
> 
>  ??? def validScale(self, scaleName):
>  ??????? if scaleName.upper == 'F' or 'C' or 'K':
>  ??????????? return True
>  ??????? else:
>  ??????????? return False
> 
>  ??? def convertTemp(self):
>  ??????? """ Converts temperature scale if scales valid."""
>  ??????? if validScale(self.scale):
>  ??????????? scaleValid = True
>  ??????? if validScale(self.newScale):
>  ??????????? newScaleValid = True
>  ??????? if scaleValid and newScaleValid:
>  ??????????? print('Scale converted')
>  ??????? else:
>  ??????????? msg = "There was and error with the scales entered.\n"
>  ??????????? msg = msg + "You entered: " + self.scale
>  ??????????? msg = msg + ' ' 'and' + self.newScale
>  ??????????? print(msg)
> 
> if __name__ == "__main__":
>  ??? myclass = TempConverter(32, 'f', 'c')
>  ??? myclass.convertTemp()

Thanks all for your (ready, wait for it) self-lessness help (get it?)

Dave (laughing)