Question

Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors.

Remember the rules:

1. Rock beats scissors

2. Scissors beats paper

3. Paper beats rock

The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an invalid choice, the program should inform the user.

Your program should contain two functions:

(1) compare_answers(u1, u1a, u2, u2a): This function takes four parameters (u1, u1a, u2, u2a), and returns a string. The parameters are all strings: u1(player 1 name), u1a (player 1 choice), u2 (player 2 name), and u2a(player 2 choice). The returned string should include the result of the game. (Submit for 6 points)

For example:

1. It's a tie!

2. Invalid input! You have not entered rock, paper or scissors, try again.

3. Rock wins! Good job 'u1'

4. Scissors win! Good job u2

(2) __main__: In the main, you do the following: (Submit for 4 points)

a. Prompt the user to input the name of player 1.

b. Prompt the user to input the name of player 2.

c. Prompt the user to input the choice of player 1.

d. Prompt the user to input the choice of player 2.

e. Print the result of the game.

Here are some example runs of the program:

Example 1: Player 1 inputs an invalid choice.

# FIXME 1: Complete the compare_answers function
def compare_answers(u1, u1a, u2, u2a):
    if u1a == u2a:
        pass
    elif u1a == 'rock':
        if u2a == 'scissors':
            return("Rock wins! Good job " + u1)
        elif u2a == 'Paper':
            pass
        else:
            pass
    
    # You need more elif in here
    
    else:
        pass


if __name__ == "__main__":
    # FIXME 2a: Prompt the user to input the name of player 1
    pass
    # FIXME 2b: Prompt the user to input the name of player 2

    # FIXME 2c: Prompt the user to input the choice of player 1

    # FIXME 2d: Prompt the user to input the choice of player 2

    # FIXME 2e: Call the function and print the result of the game
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of program code:-

Screenshot of output:-

Program code to copy:-

#Function takes four parameters and returns a string.
#The parameters are all strings: u1(player 1 name), u1a (player 1 choice),
#u2 (player 2 name), and u2a(player 2 choice).
#The returned string includes the result of the game.
def compare_answers(u1, u1a, u2, u2a):
if (u1a == u2a):
return "It's a tie!"
elif (u1a == "rock" and u2a == "paper"):
return "Paper wins!Good job "+u2
elif(u1a == "paper" and u2a == "rock"):
return "Paper wins!Good job "+u1
elif(u1a == "paper" and u2a == "scissors"):
return "Scissors wins!Good job "+u2
elif(u1a == "scissors" and u2a == "paper"):
return "Scissors wins!Good job "+u1
elif(u1a == "scissors" and u2a == "rock"):
return "Rock wins!Good job "+u2
elif(u1a == "rock" and u2a == "scissors"):
return "Rock wins!Good job "+u1
else:
return "Invalid input! You have not entered rock, paper or scissors, try again."

#main() function
def main():
#Prompt & read name of player1 from user
player1Name = input('Enter the name of player1: ')
#Prompt & read name of player2 from user
player2Name = input('Enter the name of player2: ')
#Prompt & read choice of player1 from user
#The choice entered by the user converted into lowercase using lower() function
player1Choice = input('Enter player1 choice(rock,paper,scissors):').lower()
#Prompt & read choice of player1 from user
player2Choice = input('Enter player2 choice(rock,paper,scissors):').lower()

#Calling function to get the result of game and print the result
print(compare_answers(player1Name,player1Choice,player2Name,player2Choice))

#Calling main() function
if __name__== "__main__":
main()

Add a comment
Know the answer?
Add Answer to:
Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in python that lets the user play the game Rock, Paper, Scissors against...

    Write a program in python that lets the user play the game Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...

    JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There are two players. Each player secretly chooses either, paper, rock or scissors. At the count of three, each player reveals what they have chosen. The winner of the game is determined this way: paper beats rock (because paper covers rock) rock beats scissors (because rock crushes scissors) scissors beat paper (because scissors cut paper) If the two players choose the same item, it is...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • (c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, the user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard using a menu in a function, userChoice, that returns a character. Next, there should be a function, computerChoice, that generates the computer’s play. A random number in the range of 1 through 3 is generated. If the...

  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

  • Write a MARIE program to implement one round of rock paper scissors game. Your program should...

    Write a MARIE program to implement one round of rock paper scissors game. Your program should represent the three moves ‘rock’, ‘paper’ and ‘scissors’ with numbers 1, 2 and 3 respectively. When the program is run, there should be two input prompts, one after the other, to ask for player 1’s and player 2’s moves (a number 1, 2 or 3). Then the program would compare both numbers and apply the following rules to work out the winner. Paper beats...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT