Найти - Пользователи
Полная версия: не работает elif
Начало » Python для новичков » не работает elif
1
shiyri
Добрый день, почему то перестал работать elif, не переходит хотя по условию должно
 #!/usr/bin/python3
#coding: utf-8
import paho.mqtt.client as mqtt
import time
import serial
def on_connect(client, userdata, flags, rc):
        print("Start controller")
        client.subscribe("office/garage_door/state")
        client.subscribe("checkout/pass")
        client.publish("office/garage_door/state", "C")
def on_message(client, userdata, msg):
        print(str(msg.payload))
        if (msg.topic) == "office/garage_door/state":
            if str(msg.payload) == "b'O'":
                    port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=1)
                    port.write(b'AT\r')
                    time.sleep(0.5)
                    print(msg.topic)
                    port.write(b'ATD0000000000;\r')
                    time.sleep(10)
                    port.write(b'AT+CHUP\r')
                    port.close()
                    time.sleep(5)
                    client.publish("office/garage_door/state", "C")
        elif (msg.topic) == "checkout/pass":
             print(str(msg.payload))
             numer = str(msg.payload)[2:14]
             print(numer)
             pas = str(msg.payload)[15:-1]
             print(pas)
             pasm = " parol: "
             port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=1)
             port.write(b'AT\r')
             time.sleep(0.5)
             port.write(b'ATZ\r')
             time.sleep(0.5)
             port.write(b'AT+CMGF=1\r')
             time.sleep(0.5)
             port.write(b'AT+CMGS="' + numer.encode() + b'"\r')
             time.sleep(0.5)
             port.write((pasm + pas).encode() + b"\r")
             time.sleep(0.5)
             port.write(bytes([26]))
             time.sleep(0.5)
             port.close()
client = mqtt.Client("digi_mqtt_test")
client.on_connect = on_connect
client.on_message = on_message
client.connect('127.0.0.1', 1883)
client.loop_forever()
doza_and
shiyri
почему то перестал работать elif
Вы имеете ввиду что интерпретатор сломали? Ой врядли.

В данном случае спасение утопающего дело рук утопающего. Используйте отладчик или логгируйте сообщения и проводимые операции. Сторонние люди только по логу и могут чтото сказать.

Крайне рекомендую сделать финальный else возбуждающий ошибку или выводящий сообщение. Это в жизни часто помогает.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB