Home > Enterprise >  How can I print else or if statement only once when the condition is meet instead of infinite withou
How can I print else or if statement only once when the condition is meet instead of infinite withou

Time:12-30

In the below python code I am checking if google is up when it is up its printing infinite amount of time and same goes to else statement what I want is if the if statement is false print the else statement only one time until the if statement goes true and when if statement goes true print the if statement only one time vice versa printing everything onetime until one become true again

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!')

CodePudding user response:

You can add variable for checking last value. E.g. if response is 0, setting variable value and print. 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

CodePudding user response:

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. E.g., before the while statement, initialize two variables, e.g., 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. And likewise with first.false inside the else statement.

CodePudding user response:

You can add a break statement to exit while loop, for example like that:

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!!

CodePudding user response:

You can create an update function with a static last_update string. The update function will not print the same output twice.

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!")
  • Related