Python Forum
How to start the program from the beginning.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to start the program from the beginning.
#6
Your code is upside down. Instead of a function that asks if you want to play again you should have a function that plays the game.
import random

choices = "RPS"
win = {"R":"S", "P":"R", "S":"P"}
throws = {"R":"Rock", "P":"Paper", "S":"Scissor"}

def get_input(prompt, choices):
    """Get user input.  Repeat until valid input is entered."""
    while True:
        choice = input(prompt).upper()
        if choice in choices:
            return choice

def play_game():
    """Play Rock Paper Scissor against computer until there is a winner."""
    while True: 
        computer = random.choice(choices)
        player = get_input("\nChoose [R]ock, [P]aper or [S]cissor: ", choices)

        if player == computer:
            print(f"Computer throws {throws[computer]}. Draw!")
        elif win[player] == computer:
            print(f"Computer throws {throws[computer]}. You Win!")
            break
        else:
            print(f"Computer throws {throws[computer]}. You lose.")
            break

# Play until player wants to quit
while True:
    play_game()
    if get_input("\nDo you want to play again? [Y,N]", "YN") != "Y":
        break
Since user input figures so importantly in the game I wrote a function to simplify getting valid input.
iamaghost likes this post
Reply


Messages In This Thread
RE: How to start the program from the beginning. - by deanhystad - Feb-23-2021, 03:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Just beginning dalekeel 2 286 Apr-15-2024, 11:49 AM
Last Post: Pedroski55
  Program doesnt return beginning bilisim19 2 968 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  List of dataframe values beginning with x,y or z glidecode 3 1,985 Nov-08-2021, 10:16 PM
Last Post: glidecode
  Start my program in RPI4B from SSH and Failed ATARI_LIVE 3 3,565 Nov-11-2020, 11:19 AM
Last Post: ATARI_LIVE
  Problem: Once I cancel the process my program will start working! Hadad 0 1,670 Jul-24-2019, 04:09 PM
Last Post: Hadad
  want to change the beginning of the result Rudinirudini 5 3,640 Nov-15-2018, 11:28 AM
Last Post: Rudinirudini
  How can I start a python program out of a python program? peer 3 2,345 Oct-30-2018, 05:07 PM
Last Post: Larz60+
  What's the difference b/w assigning start=None and start=" " Madara 1 2,349 Aug-06-2018, 08:23 AM
Last Post: buran
  Beginning of Beginner Help: Should I start with Python? appdevelnewb 2 3,100 Jul-23-2018, 11:17 PM
Last Post: appdevelnewb
  Wrap from end to beginning. 27 to 1, 28 to 2 etc DreamingInsanity 5 3,738 Jun-24-2018, 01:02 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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