Python Forum

Full Version: (beginner) How do I insert a restart game option?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there. I started learning code yesterday and my second program is a dice rolling game. The object is to roll a higher number than the computer. The game is of course completely random.
I would like to have an option at the end of the game which says: "do you want to restart? Yes or No". If the user types yes, the game restarts (and this an infinite number of times, until the user has had enough of the game and ragequits xD) . I know you can do this with a loop, but how?
Excuse my lack of experience,
thanks in advance,
me.



import random

print("Dice Game: try to roll a bigger number than the computer! Good luck!")

print("Type 'go' to roll")
dieroll = input()
if dieroll == 'go':
    myNumber = random.randint(1,6)
    pcNumber = random.randint(1,6)
    print("You rolled " + str(myNumber))
    print("He rolled " + str(pcNumber))
    if myNumber < pcNumber:
        print("You lose!")
    if pcNumber < myNumber:
        print("You win!")
Hello, nice start to your coding adventure!
I see you are familiar with if statements. Next thing I suggest you to learn is loops. If statements are used (mostly) when a decision needs to be taken, and loops are used when there is repetition of a task. So there you will quickly find a way to accomplish your goal. Feel free to post again if you will need further tips =)

P.S.:
What happens if both, player and computer, roll same number?
I suggest looking into elif/else statements alongside "if".