Average User Average User -4 years ago 50
Python Question

How to simplify this python script?

I'm making a script in python and I'm new to python. Could someone help me simplify this.

if data[-8]=="B" and data[-5]=="1" and data[-4]=="6":
print("Button1")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="7":
print("Button2")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="8":
print("Button3")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="9":
print("Button4")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="A":
print("Button5")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="B":
print("Button6")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="C":
print("Button7")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="D":
print("Button8")


EDIT: I made a mistake with the button sequence.

Answer Source
if data[-8]=="B" and data[-5]=="1" and data[-4] in "6789ABCD":
    print("Button1")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download