(Python 3.10.4)
I have a for condition and inside this for a have a function that need a break and go back to the top like "continue" do, but i can't use break outside a loop, how can i go back to the top of the code? I will use this function so many times after and i don't want to have so many if in my code
import time
def function1():
if pyautogui.locateCenterOnScreen('file.png')
pyautogui.moveTo(x=612, y263)
time.sleep(1)
break
How can i break the code in this case:
for c in range (0,10):
print('begin for')
#FUNCTION
function1():
#
#
#REST OF THE CODE
CodePudding user response:
It's hard to imagine what you meant by 'break the code' with your incorrect code indentations. I guess you meant to break the for loop when the function breakes.
Use a variable out side the function to indicate when you're done with your function.
And check for the value of the variable within the for loop to break or pass
br = 0
def function1():
if pyautogui.locateCenterOnScreen('file.png')
pyautogui.moveTo(x=612, y=263)
br = 1
time.sleep(1)
return 0
for c in range (0,10):
print('begin for')
#FUNCTION
function1()
if br == 1:
break
else:
pass
#
#
#REST OF THE CODE
CodePudding user response:
If you want to jump out of a function, use return please.
For example
def test():
for i in range(10):
print(i)
if i == 5:
return i
Output
0
1
2
3
4
5