Home > front end >  How do I add a feature that asks user if they would like to play again?
How do I add a feature that asks user if they would like to play again?

Time:05-05

Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game

ask_p1 = input("Player 1, Enter rock/paper/scissors: ")
ask_p2 = input("Player 2, Enter rock/paper/scissors: ")

def rock_paper_scissors(p1, p2):
    a_list = ["rock", "paper", "scissors"]

    while p1 == p2:
        print ("Nobody wins. Try again")
        p1 = input("Player 1, Enter rock/paper/scissors: ")
        p2 = input("Player 2, Enter rock/paper/scissors: ")

    if p1 == "rock" and p2 == "paper":
        print ("Player 2 wins!")

    elif p1 == "paper" and p2 == "rock":
        print ("Player 1 wins!")

    elif p1 == "rock" and p2 == "scissors":
        print ("Player 1 wins!")

    elif p1 == "scissors" and p2 == "rock":
        print ("Player 2 wins")

    elif p1 == "paper" and p2 == "scissors":
        print ("Player 2 wins!")

    elif p1 == "scissors" and p2 == "paper":
        print ("Player 1 wins!")

rock_paper_scissors(ask_p1, ask_p2)

CodePudding user response:

You can make the function call itself, something like the example below.

ask_p1 = input("Player 1, Enter rock/paper/scissors: ")
ask_p2 = input("Player 2, Enter rock/paper/scissors: ")

def rock_paper_scissors(p1, p2):
    a_list = ["rock", "paper", "scissors"]

    while p1 == p2:
        print ("Nobody wins. Try again")
        p1 = input("Player 1, Enter rock/paper/scissors: ")
        p2 = input("Player 2, Enter rock/paper/scissors: ")

    if p1 == "rock" and p2 == "paper":
        print ("Player 2 wins!")
        user_Input = input("Do you want to play again? Y or N")

    elif p1 == "paper" and p2 == "rock":
        print ("Player 1 wins!")
        user_Input = input("Do you want to play again? Y or N")
    elif p1 == "rock" and p2 == "scissors":
        print ("Player 1 wins!")
        user_Input = input("Do you want to play again? Y or N")
    elif p1 == "scissors" and p2 == "rock":
        print ("Player 2 wins")
        user_Input = input("Do you want to play again? Y or N")
    elif p1 == "paper" and p2 == "scissors":
        print ("Player 2 wins!")
        user_Input = input("Do you want to play again? Y or N")
    elif p1 == "scissors" and p2 == "paper":
        print ("Player 1 wins!")
        user_Input = input("Do you want to play again? Y or N") 

rock_paper_scissors(ask_p1, ask_p2)

and then call the function again if you want to play again (put this at the bottom of the function)

if user_Input == 'Y':
   rock_paper_scissors(ask_p1, ask_p2)
else:
#exit or do other end code here

CodePudding user response:

Two Options, Call your function in a while loop, or call the function itself recursively.

1: Call the function in a while loop

while True:
    ask_p1 = input("Player 1, Enter rock/paper/scissors: ")
    ask_p2 = input("Player 2, Enter rock/paper/scissors: ")
    rock_paper_scissors(ask_p1, ask_p2)
    if input('Play Again Y/N')=='N':
        break

    

2:Call the function recursively. For every statement that ends, ask to play again

def PlayAgain:
    if input('Play Again Y/N')=='Y':
        rock_paper_scissors(ask_p1, ask_p2)

#In the rock_paper_scissors(ask_p1, ask_p2) function
...
if p1 == "rock" and p2 == "paper":
        print ("Player 2 wins!")
        PlayAgain()
...

    

CodePudding user response:

I don't think some of the variables that you are assigning are being used, nor is there much of a need to add players as arguments in the function, since they never come up in the program. I would put everything inside the function and then recursively call it again when they player is asked if they would like to play another game.

def rock_paper_scissors():
    p1 = input("Player 1, Enter rock/paper/scissors: ")
    p2 = input("Player 2, Enter rock/paper/scissors: ")

    while p1 == p2:
        print ("Nobody wins. Try again")
        p1 = input("Player 1, Enter rock/paper/scissors: ")
        p2 = input("Player 2, Enter rock/paper/scissors: ")

    if p1 == "rock" and p2 == "paper":
        print ("Player 2 wins!")

    elif p1 == "paper" and p2 == "rock":
        print ("Player 1 wins!")

    elif p1 == "rock" and p2 == "scissors":
        print ("Player 1 wins!")

    elif p1 == "scissors" and p2 == "rock":
        print ("Player 2 wins")

    elif p1 == "paper" and p2 == "scissors":
        print ("Player 2 wins!")

    elif p1 == "scissors" and p2 == "paper":
        print ("Player 1 wins!")
        
    answer = input("Reply 'y' to play again, or 'n' to end the game.\n")
    if answer == 'y':
        rock_paper_scissors()

rock_paper_scissors()
  • Related