python同时执行if和else语句

2024-09-27 23:24:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是python我们同时执行if和else语句

请看图片 Pic of indent

我试过“如果不是”,独立于其他的,但似乎没有任何效果。在

这是我剧本的一部分。我对这件事不太熟悉,所以我很感激你的帮助。在

我要做的是,如果cofig中有255.255.255.252,我要读取。(这起作用了,并相应地改变了配置)但是如果没有255.255.255.252,我的日志中应该有一个打印。在

结果是:(两者兼而有之)

2017-10-26 15:39:29350 dailer 1关闭IP测试路由器完成 2017-10-26 15:39:29351 dailer 1 off IP lprm1212没有/30

       for my_output in result0.split("\n"):
            if len(my_output) !=0:
                if  my_output.find("255.255.255.252") != -1:
                    print (my_output)
                    tn.read_until('')
                    tn.write("config terminal"+"\n")
                    tn.read_until("(config)#")
                    tn.write("interface Dialer1"+"\n")
                    tn.read_until("(config-if)#")
                    tn.write("description test"+"\n")
                    tn.write("end"+"\n")
                    logger.debug('dailer 1 off IP ' + host2login + ' is completed')
                 else:
                     logger.debug('dailer 1 off IP ' + host2login + ' has no /30')
             else:
                 logger.debug('dailer 1 off IP ' + host2login + ' has no /30')
        else:
            logger.debug('dailer 1 off IP ' + host2login + ' has no /30')

Tags: debugipconfigreadoutputifmylogger
2条回答

你的压痕到处都是。如果和else必须位于相同的缩进位置,如:

for my_output in result0.split("\n"):
    if len(my_output) !=0:
        if  my_output.find("255.255.255.252") != -1:
            print (my_output)
            tn.read_until('')
            tn.write("config terminal"+"\n")
            tn.read_until("(config)#")
            tn.write("interface Dialer1"+"\n")
            tn.read_until("(config-if)#")
            tn.write("description test"+"\n")
            tn.write("end"+"\n")
            logger.debug('dailer 1 off IP ' + host2login + ' is completed')
        else:
            logger.debug('dailer 1 off IP ' + host2login + ' has no /30')

在这种情况下,else属于第二个if,而第一个if没有else case。在

代码的问题是缩进,如果(s)和其他(s)彼此相关“必须”处于相同的缩进级别:

# code
if x<0:
    # do something
else:
    # do something else
# code

相关问题 更多 >

    热门问题