Home > Net >  How to print change from previous printed line?
How to print change from previous printed line?

Time:11-14

I have a function that reads temperatures from a device. After printing the temperature, I'd like to print the change in temperature from the previous printed line. Is there an easy way to do this?

While i < 50000:
T = getTemps()
print(T)
print(deltaT) # <-- I want to do this 

CodePudding user response:

I think you can keep the previous temperature as a separate variable, and once you print the delta, you can reassign previous one with the current one, because anyway with next loop you will get new value;

t = getTemps()
tPrevious = t
while i < 50000:
    t = getTemps()
    print(t)
    print(t - tPrevious)
    tPrevious = t

CodePudding user response:

I think somethink like that

prev_T = 0  # Default value for first iteration
i = 0
while i < 50000:
    T = getTemps()
    print(T)
    print(T - prev_T)
    prev_T = T
    i  = 1

  • Related