vendredi 10 septembre 2021

How do I make this python loop increment an int up and down repeating

I'm trying to create a fake thermostat that has a set temperature and increments up and down between the minimum and maximum temperatures over time. The minimum is the set temperature - 2 and the max temperature is the set temperature + 1.

Here's the thermostat class:

class Thermostat:

    def __init__(self, set_temp = 70, actual_temp = 70):
        self.set_temp = set_temp
        self.actual_temp = actual_temp
        self.max = set_temp + 1
        self.min = set_temp - 2

    def set_the_temp(self, x):
        self.set_temp = x

    def increment_temp(self, x):
        self.actual_temp += x

#create a thermostat
thermostat = Thermostat()

Here's what I have for the loop that increments, but I can't figure out the logic for the conditional statements, it always just loops between 70 and 71.

def increment_temp():

    last_temp = 69

    def increment(x):
        last_temp = thermostat.actual_temp
        thermostat.increment_temp(x)
        print("set temp: ", thermostat.set_temp)
        print("actual temp: ", thermostat.actual_temp)
        print("last temp:", last_temp)


    while True:
        if thermostat.actual_temp > last_temp:
            if thermostat.actual_temp < thermostat.max:
                increment(1)
            else:
                increment(-1)
        elif thermostat.actual_temp < last_temp:
            if thermostat.actual_temp > thermostat.min:
                increment(-1)
            else:
                increment(1)
        time.sleep(randint(2,10))

for the life of me, I can't figure out why actual_temp goes back up to 71 from 70 even when actual_temp is less than set_temp when it's 71.

Aucun commentaire:

Enregistrer un commentaire