0

I was making an program in Python and I needed to break out of if else lop without breaking out of while loop.

Current code (incomplete code just for example. Not full code.):

class foo:
  def __init__(self, time1, link)
        while self.isItNow == False:
            self.current_time = self.now.strftime("%H:%M")
            print(self.current_time)
            if str(self.current_time) == self.time1:
                webbrowser.open(self.link, new=1)
                self.isItNow = True
            else:
                print("Current Time =", self.current_time,". Retrying again in 5 seconds")
                time.sleep(5)
                break

I wanted to break the else and start the if loop again but I can't do it because it breaks out of the while loop.

Thanks in advance!!!!

1
  • 2
    if..else is not a loop.... Commented Jun 16, 2021 at 4:50

2 Answers 2

2

Try using continue statement https://www.tutorialspoint.com/python/python_loop_control.htm#:~:text=The%20continue%20statement%20in%20Python,both%20while%20and%20for%20loops.

Sign up to request clarification or add additional context in comments.

Comments

1

Else is not a loop, but you can immediately go to the next loop iteration by using continue.

Also... If you just want to break out of the else statement, I'm not sure why you do because do don't have any code below that line...

2 Comments

Alright just wait then it's not good to ask new questions in comments. Also while that cooldown is happening you should take some time to restructure your code, there are a lot of better ways to do what you're trying to accomplish. Have fun programming!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.