[英]python executing both if and else statement
I have the problem that python us executing both if and else statement together我有一个问题,python 我们一起执行 if 和 else 语句
Please watch pic Pic of indent请看缩进图片
I tried 'if not' and indendent the else but nothing seems to work.我尝试了“如果不是”并缩进其他但似乎没有任何效果。
Here is a part of my script.这是我的脚本的一部分。 Im litte new to this so i would appreciate the help.
我对此很陌生,因此我将不胜感激。
What i want to do is read if there is a 255.255.255.252 in the cofig.我想要做的是读取配置文件中是否有 255.255.255.252。 (this works and does change the config accordingly) But if there is no 255.255.255.252 there should be a print in my log.
(这有效并且相应地更改了配置)但是如果没有 255.255.255.252 应该在我的日志中打印。
the result is : (it does both)结果是:(两者兼而有之)
2017-10-26 15:39:29,350 dailer 1 off IP testrouter is completed 2017-10-26 15:39:29,351 dailer 1 off IP lprm1212 has no /30 2017-10-26 15:39:29,350 dailer 1 off IP testrouter 完成 2017-10-26 15:39:29,351 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')
You have the indentation all over the place.你到处都有缩进。 If and else have to be on the same indentation like so:
If 和 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')
In this case, your else belongs to the second if, while the first if does not have a else case.在这种情况下,您的 else 属于第二个 if,而第一个 if 没有 else 情况。
The problem with your code is indentation , if(s) and else(s) which relate to each other "MUST" be in the same indentation level :您的代码的问题是缩进,相互关联的 if(s) 和 else(s)“必须”处于相同的缩进级别:
# code
if x<0:
# do something
else:
# do something else
# code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.