Python Forum
py 2.7 restarting the program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
py 2.7 restarting the program
#1
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()
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 624 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  Skipping line in text without Restarting Loop IdMineThat 4 1,477 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Restarting a script new2codez 0 1,124 Nov-11-2020, 11:02 AM
Last Post: new2codez
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,446 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  Restarting code from scratch palladium 1 1,998 Feb-29-2020, 04:46 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020