Question

In: Computer Science

I'm working on a python programming problem where I have to write a program that plays...

I'm working on a python programming problem where I have to write a program that plays rock paper scissors.

our program will allow a human user to play Rock, Paper, Scissors with the computer. Each round of the game will have the following structure:

  • The program will choose a weapon (Rock, Paper, Scissors), but its choice will not be displayed until later so the user doesn’t see it.
  • The program will announce the beginning of the round and ask the user for his/her weapon choice
  • The two weapons will be compared to determine the winner (or a tie) and the results will be displayed by the program
  • The next round will begin, and the game will continue until the user chooses to quit
  • The computer will keep score and print the score when the game ends

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 or r (Rock)
  • P or p (Paper)
  • S or s (Scissors)
  • Q or 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)

Solutions

Expert Solution

hi ,

please find program and out attached below.

kindly upvote.

import random

rockCount = 0
paperCount = 0
sciCount = 0
compWinCt = 0
userWinCt = 0
drawCt = 0
comp_choice = "p"


def startOfProgram():
  
play_game = input("wanna play again\n"
"y/n").lower()
if play_game == "y" or play_game == "yes" or play_game == "Y":
main()
elif play_game == "n" or play_game == "no" or play_game == "N":
print("Computer won", compWinCt, "times.")
print("User won", userWinCt, "times.")
print("Draw games count is: ",drawCt)
print("User picked Rock", rockCount, "times.")
print("User picked Paper", paperCount, "times.")
print("User picked Scissors", sciCount, "times.")
quit()
else:
print("not a valid choice, try again")
startOfProgram()

def RandomChoice():
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice = "r"
elif comp_choice == 2:
comp_choice = "p"
elif comp_choice == 3:
comp_choice = "s"
return comp_choice


def ComputerChoice():
if rockCount == 0 and paperCount == 0 and sciCount == 0:
return RandomChoice()
else:
if (rockCount > paperCount) and (rockCount > sciCount):
comp_choice == "r"
elif (paperCount > rockCount) and (paperCount > sciCount):
comp_choice == "p"
elif (sciCount > paperCount) and (sciCount > rockCount):
comp_choice == "s"
return comp_choice

def main():
  
# set count for a zero
global rockCount
global paperCount
global sciCount
global compWinCt
global userWinCt
global drawCt
  
comp_choice = ComputerChoice()
  
user_choice = input("Please enter from menu below \n"
"r/R for rock,\n"
"p/P for paper \n"
"s/S for scissors \n"
"q/Q to quit ")
  
if comp_choice == "r" :
if user_choice == "r" or user_choice == "R":
print("match draw")
rockCount += 1
drawCt += 1
elif user_choice == "p" or user_choice == "P":
print("user won. user:paper vs computer:Rock")
userPaperCt += 1
userWinCt += 1
elif user_choice == "s" or user_choice == "S":
print("user loose. user:Scissors vs Computer:Rock")
sciCount += 1
compWinCt += 1
else:
print("Try again")
startOfProgram()
  

if comp_choice == "p":
if user_choice == "r" or user_choice == "R":
print("user loose. user:Rock vs Computer:Paper")
rockCount += 1
compWinCt += 1
elif user_choice == "p" or user_choice == "P":
print("draw match")
paperCount += 1
drawCt += 1
elif user_choice == "s" or user_choice == "S":
print("user won. user:Scissors vs computer:Paper")
sciCount += 1
userWinCt += 1
startOfProgram()
  
if comp_choice == "s":
if user_choice == "r" or user_choice == "R":
print("user won. user:Rock vs Computer:Scissors")
rockCount += 1
userWinCt += 1
elif user_choice == "p" or user_choice == "P":
print("user loose. user:Paper vs Computer:Scissors")
paperCount += 1
compWinCt += 1
elif user_choice == "s" or user_choice == "S":
print("draw match")
sciCount += 1
drawCt += 1
startOfProgram()


main()

thanks


Related Solutions

I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
Using Python Programming PROBLEM 3 - OLD MACDONALD: Write a program to print the lyrics of...
Using Python Programming PROBLEM 3 - OLD MACDONALD: Write a program to print the lyrics of the song ”Old MacDonald”. Your program should print the lyrics using five different animals (i.e., cow, sheep, dog, cat ,turkey) using the example verse below. For this problem you are required to write then make use of a function getVerse that takes two string parameters: (1) the name of an animal and (2) the sound that animal makes. The function should not print anything...
I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
Here is the problem I am working on. I need to write a py program that:...
Here is the problem I am working on. I need to write a py program that: Calculates a Cartesian Product Output, A x B of 2 lists. I want to limit each list to 5 numbers. It is kind of working, but I can't help think that there is a simpler way to express this problem. Please advise... Here is my code: def CartesianProductOutput(A,B):     lst = []     for x in A:         t = []         t.append(x)         for y in B:             temp...
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
I'm trying to solve a problem where I have an object resting on an inclined plane,...
I'm trying to solve a problem where I have an object resting on an inclined plane, with the angle of the plan being alpha, and the weight being w. I'm having trouble figuring out how I can calculate the component of the weight parallel to the plane. I also want to find out the weight component perpendicular to the plane. I don't want an outright answer, more of an explanation to help me understand. Thanks!
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data...
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data from a CSV file, which has no names, just numbers. All I did was to read a .csv file. How do I pull data from three columns which contains about 1500 rows with just numbers and make a scatter plot with two in the x-axis and the third in the y-axis?
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT