0

when my code exits this while loop and if statement :

if im.getpixel((258,375)) == (30,37,43):
    battle = True
    print('lets Battle!!')
    time.sleep(10)
    leftClick()
    while battle == True:
        mousePos(503,487)
        leftClick()
        leftClick()
        time.sleep(5)
        im = screenGrab()
        if im.getpixel((258,375)) != (30,37,43) and im.getpixel((258,375)) != (99, 114, 121) and im.getpixel((258,375)) != (225, 225, 225):
            battle = False
            break
    print('battle Over')
    break

The code ends the parent while loop:

while 1 == 1:
    im = screenGrab()
    if im.getpixel((258,375)) == (30,37,43):
        battle = True
        print('lets Battle!!')
        time.sleep(10)
        leftClick()
        while battle == True:
            mousePos(503,487)
            leftClick()
            leftClick()
            time.sleep(5)
            im = screenGrab()
            if im.getpixel((258,375)) != (30,37,43) and   im.getpixel((258,375)) != (99, 114, 121) and im.getpixel((258,375)) != (225, 225, 225):
                battle = False
                break
        print('battle Over')
        break
    left(2)
    right(2)

I have been trying for ages but I can't get left() and right() to run and the loop to continue after the if statement ends.

Thanks for your help.

2 Answers 2

1

You are calling breakafter your printstatement :

print('battle Over')
        break

Your first breakstatement exits the second while and the breakmentioned above exits your first while.

Simply remove the breakstatement if you do not want to exit the parent while after the if statement.

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

Comments

0

Break exits the current loop(while or for) not 'if' or 'else'. Use 'continue' to break the current iteration. And your code has 2 breaks remove the outer break and it should work as expected.

Comments

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.