Python Forum

Full Version: py 2.7 restarting the program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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): #here i want it to restart
        continue
     elif x == int(2): #and here it also says i have an error
        break
    else:
        print 'Invalid input'


roll_the_dice()
what is your question?
x = raw_input('If you want to reroll press 1 if not press 2:\n')
    if x == int(1): #here i want it to restart
        continue
     elif x == int(2): #and here it also says i have an error
        break
    else:
        print 'Invalid input'
x is a string. so, you either need to convert to int (that is what you try, but wrong way) or just compare it with str.
    if int(x) == 1: #here i want it to restart
        continue
     elif int(x) == 2: #and here it also says i have an error
        break
    else:
        print 'Invalid input'
    if x == '1': #here i want it to restart
        continue
     elif x == '2': #and here it also says i have an error
        break
    else:
        print 'Invalid input'
now, that we see where your problem was, plase note that you actually want to use a loop , e.g. while loop. it's better to limit roll_the_dice function to just that - rolling the dice and take the while loop outside of it

from random import randint
def roll_the_dice():
    dice = randint(1, 6)
    dice2 = randint(1, 6)
    return dice, dice2
    
if __name__ == '__main__':
    while True:
        user_input = raw_input('Do you want to roll the dice? (y/yes)')
        if user_input.lower() in ('y', 'yes'):
            print 'Dice1: {} and Dice2: {}'.format(*roll_the_dice())
        else:
            break
Finally, because you are new to python - you should use python3, not python2