-4
from random import randint
def roll_the_dice():
    dice = randint(1, 6)
    dice2 = randint(1, 6)
    print dice , dice2
    x = raw_input('If you want to reroll press 1 if not press 2:\n')
    if x == int(1):
        continue
    elif x == int(2):
        break
    else:
        print 'Invalid input'

roll_the_dice()

The problem is where i put the continue , how do i make it restart depend on the answer

John
  • 1
  • 2
  • 2
    Use a `while` loop? `break` and `continue` don't make much sense without loops – UnholySheep Jul 06 '18 at 07:33
  • You could call roll_the_dice() instead of continue. – Anatolii Jul 06 '18 at 07:34
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Jul 06 '18 at 07:35
  • i tried it now its not working i think you cant use the function you defining inside it – John Jul 06 '18 at 07:37
  • You'd be better off using a while loop. Using recursion for input validatation like this is a hack. – khelwood Jul 06 '18 at 07:38

1 Answers1

0

If you want it to run until a specific case, you can put it inside a while true loop. When using break and continue, it's in context of a loop. It could be a for loop or any kind of loop, but if you want the loop to run forever (Or until you break it) you can use while true.

Like this:

from random import randint

def roll_the_dice():
    while True:
        dice = randint(1, 6)
        dice2 = randint(1, 6)
        print dice , dice2
        x = raw_input('If you want to reroll press 1 if not press 2:\n')
        if x == int(1):
            continue
        elif x == int(2):
            break
        else:
            print 'Invalid input'

roll_the_dice()
Panto
  • 104
  • 1
  • 1
  • 9