Zach Thomas
Zach Thomas

Reputation: 147

print out if two conditions are met

What I want to do is modify the if statement that I have below for dT. Right now I have it printing out "Temperature" "dT" "Steady State" if dT is between -.5 and .5 but instead I want it to print out those three items only if the dT value is between -.5 and .5 for ten straight iterations. If not, it should just print out "Temperature" "dT" as the else statement suggests.

temperature = []
dT_tol = .5

# Read loop
for i in range(60):
    # Get the thermocouple reading on AIN0. 
    tempC = ljm.eReadName(handle, "AIN0_EF_READ_A")
    temperature.append(tempC)
    dT = temperature[i]-temperature[i-1]

    if -dT_tol<dT<dT_tol:
        print "Temperature:","%.3f"% temperature[i],"         " "dT:", "%.3f"% dT, "         " "Steady State" 
        sleep(1)
    else:
        print "Temperature:","%.3f"% temperature[i],"         " "dT:", "%.3f"% dT
        sleep(1)

Upvotes: 1

Views: 152

Answers (3)

joel goldstick
joel goldstick

Reputation: 4493

I think this is what you might want:

I am editing my answer with new code. If this is the wrong thing to do, I'll delete this answer and add a new one. My mind was close but kind of messy.

This code first finds all the places where the data is out of range and records their indices (stops). It then checks from 'stop' to stop' to see if the range is greater than 2 and collects those values in the final results.

temp_list = []

# first make up some data.  These are all positive, between 0 and .7.  In your case you would use your temperature data
temp_list = [.6, .5, .4, .3, .5, .6, .4, .3, .2, .8, .4]
print "temp_list"
print temp_list

# we want to print data where at least 3 in a row are within -.5 and .5

print "---------"


results = []
stops = [(i, d) for i, d in enumerate(temp_list) if abs(d)>.5]
stops.append([len(temp_list), 10])
last_stop = 0
print stops

for pos in stops:
   if pos[0]-last_stop>2:
       results.extend(temp_list[last_stop+1:pos[0]])
       last_stop = pos[0]
       print results

print "done"

Results:

temp_list
[0.6, 0.5, 0.4, 0.3, 0.5, 0.6, 0.4, 0.3, 0.2, 0.8, 0.4]
---------
[(0, 0.6), (5, 0.6), (9, 0.8), [11, 10]]
[0.5, 0.4, 0.3, 0.5]
[0.5, 0.4, 0.3, 0.5, 0.4, 0.3, 0.2]
done

Upvotes: 1

PatNowak
PatNowak

Reputation: 5812

Just use if instead of for. Also use 0.5 instead of .5 for readability. Consider using format method for displaying strings, espesially if you're using Python 3x.

Upvotes: 1

Hassan Assaf
Hassan Assaf

Reputation: 1

You can use a count variable, set the count variable to 0 whenever the condition has been hit 10 times in a row or the streak is broken

Upvotes: 0

Related Questions