Python Forum
(beginner) How do I insert a restart game option?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(beginner) How do I insert a restart game option?
#1
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!")
Reply
#2
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".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Higher Lower Game wallytan 2 1,585 Sep-29-2022, 05:14 PM
Last Post: deanhystad
Question Beginner Boolean question [Guessing game] TKB 4 2,297 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,771 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  Using a button to kill and restart a script duckredbeard 3 3,315 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  Tic-Tac game (Beginner's coding) Shahmadhur13 5 3,113 Aug-29-2020, 08:40 PM
Last Post: deanhystad
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,740 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Need help with rpsls game(Beginner corner) Bulxy88 1 1,662 Jun-01-2020, 02:42 PM
Last Post: pyzyx3qwerty
  Hotkey to restart code? DannyB 1 2,749 May-20-2020, 02:52 AM
Last Post: michael1789
  Kernel needs to restart ErnestTBass 0 2,348 May-06-2020, 08:37 PM
Last Post: ErnestTBass
  How to restart Python after input change ozstar 3 2,373 Apr-29-2020, 03:16 AM
Last Post: ozstar

Forum Jump:

User Panel Messages

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