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