Question: Python question: I was asked to build a program for rock, paper. Scissors game. I was able to build it except i was missing one
Python question: I was asked to build a program for rock, paper. Scissors game. I was able to build it except i was missing one thing that i did not know how to do it, which is the highlighted part below. The computer that to keep track of how many times the player choses the same answer so the computer can beat the user. I'm not sure what it is maybe dictionaries? please provide the code for that. Here is my code:
import random import sys #begin the game and then loop after the first play.
def play(): while True: p_choice = input("What do you choose? ") cpu_random = random.randint(1,3) cpu_choice = cpu_random if cpu_random == 1: cpu_choice = "rock" elif cpu_random == 2: cpu_choice = "paper" elif cpu_random == 3: cpu_choice = "scissors"
#Compare the data given by the user to the CPU
def compare():
play_again = None #Tie outcome if p_choice == cpu_choice: print("Tie!") play_again = input("Play again? ")
#Rock outcome
elif p_choice == "rock" and cpu_choice == "paper": print("You Lose!") play_again = input("Play again? ") elif p_choice == "rock" and cpu_choice == "scissors": print("You Win!") play_again = input("Play again? ")
#Paper outcome
elif p_choice == "paper" and cpu_choice == "scissors": print("You Lose!") play_again = input("Play again? ") elif p_choice == "paper" and cpu_choice == "rock": print("You Win!") play_again = input("Play again? ")
#Scissors outcome
elif p_choice == "scissors" and cpu_choice == "rock": print("You Lose!") play_again = input("Play again? ") elif p_choice == "scissors" and cpu_choice == "paper": print("You Win!") play_again = input("Play again? ")
#Ask if you want to play again, then give input
if play_again == "yes": play() elif play_again == "no": print("Game Over") sys.exit() else: print("Please try again") play_again = input("play again? ") return play_again
compare()
#ask if player wants to start def game_start(): while True: begin = input("Would you like to play Rock, Paper, Scissors? ") if begin == "yes": play() return begin while begin != "yes": if begin == "no": print("Game Over.") return begin else: print("Please try again") break
game_start()

The computer should select the weapon most likely to beat the user, based on the user's previous choice of weapons. For instance, if the user has selected Paper 3 times but Rock and Scissors only 1 time each, the computer should choose Scissors as the weapon most likely to beat Paper, which is the user's most frequent choice so far To accomplish this, your program must keep track of how often the user chooses each weapon. Note that you do not need to remember the order in which the weapons were used. Instead, you simply need to keep a count of how many times the user has selected each weapon (Rock, Paper or Scissors). Your program should then use this playing history (the count of how often each weapon has been selected by the user) to determine if the user currently has a preferred weapon; if so, the computer should select the weapon most likely to beat the user's preferred weapon. During rounds when the user does not have a single preferred weapon, the computer may select any weapon. For instance, if the user has selected Rock and Paper 3 times each and Scissors only 1 time, or if the user has selected each of the weapons an equal number of times, then there is no single weapon that has been used most frequently by the user; in this case the computer may select any of the weapons. At the beginning of the game, the user should be prompted for his/her input. The valid choices for input are: R orr (Rock P or p (Paper) S or s (Scissors) .Qor q (Quit) At the beginning of each round your program should ask the user for an input. If the user inputs something other than r, R, p, P, s, s, q or Q, the program should detect the invalid entry and ask the user to make another choice. Your program should remember the game history (whether the user wins, the computer wins, or the round is tied) At the end of the game (when the user chooses 'q or 'Q), your program should display the following: The number of rounds the computer has won The number of rounds the user has won The number of rounds that ended in a tie The number of times the user selected each weapon (Rock, Paper, Scissors)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
