简体   繁体   中英

Handling different data types in while loop

I am using a sensor that provides float data. In case of a faulty measurement the sensor also sometimes returns a "None" value. I would like to collect the sensor data as long as it is in between a certain range:

def measure():
    
    data = np.array([[sensor[0], sensor[1], sensor[2]]])

    while sensor[0] > 0.0 and sensor[0] < 90.0
        
        data = np.append(data, [[sensor[0], sensor[1], sensor[2]]], axis=0)

    return data

As soon as the sensor returns a None value, an error comes up because the while command cannot handle different data types:

TypeError: '>'not supported between instances of 'NoneType' and 'float'*

I tried to include a "is not None" condition in the while loop but with no success. Can anybody help with an alternative approach or a workaround to this problem?

Based on your comment I think you want to reconstruct your function and while loop as

def measure():
    while True: 
        if sensor[0] is None: 
            # Handle however you see fit
            pass
        elif sensor[0] > 0.0 and sensor[0] < 90.0: 
            data = np.append(data, [[sensor[0], sensor[1], sensor[2]]], axis=0)
        else: 
            break
    return data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM