简体   繁体   English

如何在满足条件而不是无限的情况下仅打印一次 else 或 if 语句而不中断

[英]How can I print else or if statement only once when the condition is meet instead of infinite without breaking

In the below python code I am checking if google is up when it is up its printing "google.com is up" infinite amount of times, I want to print just once and wait until it'll go down and when google goes down I want to print "google.com is down" just once and again when google goes up print "google.com is up" like wise continue the loop without breaking.在下面的 python 代码中,我正在检查 google 在打印“google.com 已启动”时是否已启动无限次,我只想打印一次并等到它 Z34D1F91FB2E514B8576FAB1A75A9想要打印“google.com is down”只是一次又一次当google上升时打印“google.com is up”一样明智地继续循环而不中断。

hostname = "google.com" #example
while True :
  response = os.system("ping -c 1 " + hostname + "> /dev/null")

#and then check the response...
  if response == 0:
    print (hostname, 'is up!')
  else :
    print (hostname, 'is down!')

You can add variable for checking last value.您可以添加变量以检查最后一个值。 Eg if response is 0, setting variable value and print.例如,如果响应为 0,则设置变量值并打印。 If response value doesn't change, not print and change variable value如果响应值没有变化,则不打印并更改变量值

Something like that:像这样的东西:

hostname = "google.com" #example
state = -1
while True :
  response = os.system("ping -c 1 " + hostname + "> /dev/null")
  if response == 0 and state != 0:
      print(hostname, 'is up!')
      state = 0
  elif state != 1:
      print (hostname, 'is down!')
      state = 1

I'm not sure what is True, but if you are in a loop, and want to do something only the first time it's triggered, and not subsequent times, then you would need to flag the first time vs. subsequent times.我不确定什么是 True,但如果您处于循环中,并且只想在第一次触发时执行某项操作,而不是后续时间,那么您需要标记第一次与后续时间。 Eg, before the while statement, initialize two variables, eg, first.true = 1 and first.false = 1. Inside the if statement, only print if first.true = 1, and after that (but still inside the if statement), set it to 0. This ensures that it will only print the first time it gets there.例如,在while语句之前,初始化两个变量,例如first.true = 1和first.false = 1。在if语句内部,只打印if first.true = 1,之后(但仍在if语句内部) ,将其设置为 0。这确保它只会在第一次到达那里时打印。 And likewise with first.false inside the else statement.同样在 else 语句中使用 first.false。

You can add a break statement to exit while loop, for example like that:您可以添加一个 break 语句来退出 while 循环,例如:

hostname = "google.com" #example
while True :
  response = os.system("ping -c 1 " + hostname + "> /dev/null")

#and then check the response...
  if response == 0:
    print (hostname, 'is up!')
    break
  else :
    print (hostname, 'is down!')
    break

However, i am not understanding your question so good, using a state variable like in the other answer should be ok too!!但是,我不太了解您的问题,使用 state 变量就像在其他答案中一样应该没问题!

You can create an update function with a static last_update string.您可以使用 static last_update字符串创建update function。 The update function will not print the same output twice.更新 function 将不会打印相同的 output 两次。

def update(st):
    if not hasattr(update, "last_update"):
        update.last_update = ""
    if update.last_update != st:
        print(st)
        update.last_update = st


while True:
    hostname = "www.google.com"
    response = os.system("ping " + hostname)

    # and then check the response...
    if response == 0:
        update(f"{hostname} 'is up!")
    else:
        update(f"{hostname} 'is down!")

In the below code its continuously pinging my home server printing 'down' only onetime when its down and waiting, When the server is up printing 'up' only onetime then again when the server goes down it'll print (down) only onetime.在下面的代码中,当它关闭并等待时,它会持续 ping 我的家庭服务器仅打印一次“关闭”,当服务器启动时仅打印一次“向上”,然后当服务器关闭时,它只会打印(关闭)一次。

hostname = "172.16.0.96" #example


while True :
    while True :
        response = os.system("ping -c 1 " + hostname + "> /dev/null")
        if response != 0:
            print("down")
            break
    while True :
        response = os.system("ping -c 1 " + hostname + "> /dev/null")
        if response == 0:
            print("up")
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM