Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to break a high level loop?

I simplified my loop issue in the code below :

So, I have a function myfunction() it contains many steps.

This function is executed by using a loop (line 28 to 30), for range(0,4) -> execute my function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The steps inside my function summarize two cases :

  • First case : If steps are correct : print('correct') and go to the next range
  • Second case : If steps are incorrect : print('incorrect') and re run the function

The issue happens in the second case, when it runs the function and it’s incorrect and after that when it re runs the function from line 17 and then it correct, I want it after that to break all the loops and go to the next range

Execute my script and test the 2 cases to understand more my issue :

Correct case : first input = 1 , second input = 1

Incorrect case : first input = 1 , second input = p and when it re runs from line 17 : first input = 1 , second input = 1

# Correct case : first input = 1 , second input = 1
# Incorrect case : first input = 1 , second input = p
def myfunction():
    user_action = int(input('Enter  number 1: '))
    try:
        user_action2 = int(input('Enter '))
        x = user_action + user_action2

        print('correct')
        pass
    except:
        print('incorrect')
        while True:
            left = 5
            if left == 5:
                print("repeat")
                myfunction()
                # Here should break all the loops and go back to line 29 for the next number range
                break
            else:
                continue
        pass


    print('this should not be printed twice in case of incorrect results')


for y in range(0,4):
    print(y)
    myfunction()
# The issue happens when it's an  Incorrect case : first input = 1 , second input = p and when it re runs from line 17, make it correct first input = 1 , second input = 1

>Solution :

I think what you need is using a return instead of a break:

    user_action = int(input('Enter  number 1: '))
    try:
        user_action2 = int(input('Enter '))
        x = user_action + user_action2

        print('correct')
        pass
    except:
        print('incorrect')
        while True:
            left = 5
            if left == 5:
                print("repeat")
                myfunction()
                #right here
                return 
            else:
                continue
        pass


    print('this should not be printed twice in case of incorrect results')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading