Comentários?
DESLIGADO = 'desligado' AQUECENDO = 'aquecendo' RESFRIANDO = 'resfriando' #-------------------------------------------------------- def converta(t): '''(str) -> str Recebe um string t representado um temperatura. Se o string representa uma temperatura em graus Farenheit a função retorna a sua representação em graus Celsius e vice-versa. ''' temp = float(t[:-1]) unid = t[-1] if unid == 'F': temp_conv = (temp - 32) * 5 / 9 unid_conv = 'C' else: # unid == 'C': temp_conv = 9 * temp / 5 + 32 unid_conv = 'F' return '%.1f' % temp_conv + unid_conv #-------------------------------------------------------- class Termostato: #--------------------------------------------- def __init__(self, t_atual, t_min, t_max): '''(Termostato) -> None Recebe temperaturas t_atual, t_min e t_max e cria um objetos Termostato. ''' self.t_atual = t_atual self.unidade = t_atual[-1] self.t_min = t_min self.t_max = t_max if float(t_atual[:-1]) < float(t_min[:-1]): self.estado = AQUECENDO elif float(t_atual[:-1]) > float(t_max[:-1]): self.estado = RESFRIANDO else: self.estado = DESLIGADO #--------------------------------------------- def __str__(self): '''(Termostato) -> str Recebe um objeto Termostato referenciado por self e retorna o string que o representa. Esse método é chamado pelas funções print() e str(). ''' return self.t_atual + ', ' + self.estado \ + " (min = " + self.t_min \ + ", max = " + self.t_max + ")" #--------------------------------------------- def atualize(self, t_atual): '''(Termostato) -> None Recebe um objeto Termostato referenciado por self e uma temperatura t_atual e atualiza a temperatura atual do objeto. A temperatura t_max pode estar em graus Farenheit ou Celsius. Depois da atualização o objeto é impresso. ''' if t_atual[-1] != self.unidade: t_atual = converta(t_atual) self.t_atual = t_atual t_min = self.t_min # apelido t_max = self.t_max # apelido if float(t_atual[:-1]) < float(t_min[:-1]): self.estado = AQUECENDO elif float(t_atual[:-1]) > float(t_max[:-1]): self.estado = RESFRIANDO else: self.estado = DESLIGADO print(self)