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)
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Eden Gibson
  • 126
  • 1
  • 11

1 Answers1

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
Nam G VU
  • 33,193
  • 69
  • 233
  • 372