Creating a variable in Python and assigning it to the last call of a function without calling the function again -
Then I am creating a very simple temperature class which can change the temperature given from Celsius to Fahrenheit and its Adverse. The problem is that I have to create a "convert" method within a client which executes if it is called directly, but one variable is not assigned. Here is my code:
class temperature: def __init __ (self, temp = none, cf = 'C'): if temp == none: self.temp = float ('0.0 'Other': Try: self.temp = float ("{0: .2f}". Exclude format (temp)): Print ('Your temperature should be a number!') If not CF ['C' 'C' 'F', 'F']: Print ('You should identify temperature as F for Fahrenheit or C Celsius!') Other: self.cf = cf.upper () def (self) : Self-self.cf == 'F': self.temp = float ("{0: .2f}" format (self.temp - 32) * 5/9)) self.cf = 'C' return Yes) elif self Cf == 'C': self.temp = float ("{0: .2f}" format ((9/5) * self.temp + 32) self.cf = 'F' returns (self ) Def __repr__ (self): return ('temperature ({}, {})'. Format (self.temp, self.cf)) Result:
& gt; T1 = temperature ()> gt; T1 temperature (0.0, c) & gt; & Gt; T2 = Temperature (100, 'F')> & Gt; & Gt; T2 temperature (100.0, F)> & Gt; T3 = T3 = Temperature ('12.5 ',' C ') & gt; & Gt; & Gt; T3 temperature (12.5, c) # Works fine till now & gt; & Gt; & Gt; T1 .convert () to Mounter (32.0, C) # when called Convert - just what I want & gt; & Gt; & Gt; T4 = t1.convert () # equals ti 't1.convert ()' without rerunning method & gt; & Gt; & Gt; T4 temperature (0.0, C) # where I lose. I want T4 to print the 'temperature (32.0, C)' ideas?
Create a new example of a temperature and
Result:
> gt; & gt; T1 Temperature ()> (1) Temperature (0.01) (CT)> 1 (1) Temperature (0.0, C)> T. Convert () Heat (32.0, c)> T1 temperature (0.0, c)> gt; & gt; t4 = t1.convert ()> > T4 temperature (32.0, c) < / Code>
Comments
Post a Comment