Skip to content Skip to sidebar Skip to footer

How Do I Insert A Restart Game Option?

I would like to have an option at the end of my dice 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 t

Solution 1:

You can wrap your game into the function for example game() and the execute it in infinite while loop. If the user says NO it exits the loop, otherwise it runs again. And so on...

whileTrue:
        game()
        restart = input('do you want to restart Y/N?')
        if restart == 'N'breakelif restart == 'Y':
            continue

Solution 2:

You can use a while loop to implement this option.

Here is a sample implementation:

import random

# Instructions for starting the gamewhileTrue:
    print("Type 'go' to roll")
    dieroll = input()
    if dieroll == 'go':
        myNumber, pcNumber = random.randint(1,6), random.randint(1,6)
        print("You rolled " + str(myNumber))
        print("He rolled " + str(pcNumber))
        if myNumber <= pcNumber:
            print("You lose!")
        else:
            print("You win!")

    print("Do you want to restart ? Yes or No")
    response = input()
    ifnot response == "Yes":
        break

Solution 3:

First you should pack your code into a function for example

defgame():
    your code

For user prompt you can use raw_input() - Python 2.x and input() in Python 3.x At the end of your program try add something like this:

answer = raw_input("Restart?")
if answer == "Yes":
    print"Leaving the game"
    sys.exit(0) # import sys module elif answer == "No":
    print"Starting new game"
    game()

Solution 4:

It's a good idea to divide your program up into separate functions for each task. It will become easy to read and modify, so a restart function can easily be created and implemented.

The function restart() returns a bool depending on the users input. The main startGame loop checks for this value each iteration of the while loop.

When the user wants to stop playing (False) the function returns and the program stops.

import random

defrollDie():
    myNumber = random.randint(1, 6)
    pcNumber = random.randint(1, 6)

    print("You rolled %d" % myNumber)
    print("The PC rolled %d" % pcNumber)

    if myNumber > pcNumber:
        return"--- You win! ---"else:
        return"--- You lose! ---"defrestart():
    user_input = input("Would you like to play again? Type 'Yes' or 'No'\n\n")

    if user_input.lower() == "no": # Converts input to lowercase for comparisonreturnFalseelif user_input.lower() == "yes":
        returnTruedefstartGame():
    whileTrue:
        user_input = input("Dice Game: Try to roll a bigger number than the computer! Good Luck \n" +
                          "Type 'go' to roll the die... \n\n")

        if user_input == "go":
             print(rollDie())
            ifnot restart():
                return

startGame()

Solution 5:

I just submitted a post on a similar issue. Let me know if this helps:

Simple Python Game Restart Solution

To fit your specific needs, you could update the restart code to read as follows to allow for a Yes/No input option.

play_again = input("Do you want to restart? Yes or No\n")

if play_again == "Yes":
    exec(open("./yourfilenamehere.py").read())
else:
    exit()

Post a Comment for "How Do I Insert A Restart Game Option?"