0

I cannot break out of this function inside of a while loop.

This is the error message

SyntaxError: 'break' outside loop

Code:

import random
 
while True:

    def roll (min, max):
        num = random.randrange(min, max)
        print(num)
        if num == 9:
            break

    roll(1,10)
0

1 Answer 1

1

This will do the trick

import random
 
while True:

    def roll (min, max):
        num = random.randrange(min, max)
        print(num)
        return num 

    num = roll(1,10)
    if num == 9:
        break
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.