mercredi 2 août 2017

Python3.x: Variable Assignment for Different Cases

I am attempting to assign different values to a variable depending on another variable. I have used if...else statements to do so. I am wondering if there is a more efficient/simpler way to accomplish this.

I am excluding lots of my code. I am analyzing data sets line by line where column[3] can be any number.

My code:

byte_3 = format(column[3], 'b').zfill(8)
alarm = int(byte_3[1:8], 2)
if byte_3[0] == '1':
    print_string += 'Alarm '
        if alarm == 0:
            stat = 'Voltage Dropout'
        elif alarm == 1:
            stat = 'Voltage Restored'
        elif alarm == 2:
            stat = 'Probe Started'
        elif alarm == 3:
            stat = 'Probe Completed'
        elif alarm == 4:
            stat = 'Cashbox Removed'
        elif alarm == 5:
            stat = 'Cashbox Restored'
    print(print_string + stat)

I would like for stat to change depending on the value assigned to alarm. Is it possible to accomplish this in a different fashion? The majority of the values assigned to alarm will be whole integers in 1-26 where each individual integer will change the assignment of stat i.e. if alarm is 0, I expect stat to be Voltage Dropout or if alarm becomes 4 I expect stat to be Cashbox Removed

Aucun commentaire:

Enregistrer un commentaire