mardi 23 novembre 2021

What is wrong with my basic function if elif statement?

Currently my script prints the same thing each time when it should be printing the corresponding string

def vin(value):
    if value == 0:
     print("vin0")
    elif value == 1:
     print("vin1")
    elif value == 2:
     print("vin2")

It always prints vin0 no matter what value is. To simplify my question I have substituted in print for another more complex function. Here is a sample of the full code:

def socket_handler(sock):
    data = sock.recv(1024)
    print("Received %d bytes from socket: %s" % (len(data), data))
    if(len(data) == 0):
        # Unmanage the socket if it has been closed
        print("Remote has closed the connection")
        midimonster.manage(None, sock)
        sock.close()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((DEVICE_IP, DEVICE_PORT))
midimonster.manage(socket_handler, s)

def vpow(value):
    if value > 0.9:
        s.send(bytes("sendir,4:1,1,38000,1,69,341,170,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,21,21,64,21,64,21,64,21,64,21,64,21,21,21,64,21,64,21,21,21,21,21,64,21,21,21,21,21,64,21,21,21,21,21,64,21,64,21,21,21,64,21,1517,341,85,21,3655\r", "utf-8"))

def vin(value):
    if value == 0:
        s.send(bytes("sendir,4:1,1,38000,1,69,341,170,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,21,21,64,21,64,21,64,21,64,21,64,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,21,21,64,21,64,21,21,21,64,21,64,21,64,21,64,21,1517,341,85,21,3655\r", "utf-8"))
    elif value == 1:
        s.send(bytes("sendir,4:1,1,38000,1,69,341,170,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,21,21,64,21,64,21,64,21,64,21,21,21,21,21,64,21,21,21,64,21,21,21,64,21,21,21,64,21,64,21,21,21,64,21,21,21,64,21,21,21,64,21,1517,341,85,21,3655\r", "utf-8"))
    elif value == 2:
        s.send(bytes("sendir,4:1,1,38000,1,69,341,170,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,21,21,64,21,64,21,64,21,64,21,64,21,64,21,64,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,21,21,64,21,1517,341,85,21,3655\r", "utf-8"))

What does my syntax need to be for the if elif at the bottom. Bear in mind what's coming in is vin0, vin1, vin2 up to vin15 and I need it to send the corresponding bytes

Aucun commentaire:

Enregistrer un commentaire