Menu

[Solved]Python Fix Program Run Import Random Userpick Function Gets Valid Choice User Returns Def Q37136642

#IN PYTHON FIX the Program to run

import random

# userPick function gets a valid choice from the user andreturns it
def userPick():
userChoice = input(“Enter a (1) for rock, (2) for paper, (3) forscissors, or (0) to quit:n”)
while(userChoice < 0 or userChoice > 3):
print(“Invalid choice. Try again.n”)
return userChoice

# compPick function gets a valid choice from the computer andreturns it
def compPick():
return random.randrange(1, 3)

# displayPick function takes a 1, 2, or 3, and returns thestring “rock”, “paper”, “scissors”
# if an invalid value is received, it returns “nothing”
def displayPick(value):
if(value = 1):
return “rock”
elif(value = 2):
return “paper”
elif(value = 3):
return “scissors”
else:
return “nothing”

# determineWinner function takes a 1, 2, or 3 from 2 players andcomparese based on:
# 1 = rock, 2 = paper, 3 = scissors
# rock beats scissors, paper beats rock, and scissors beatspaper
# The function returns a 1 if player 1 wins, 2 if player 2 wins,and 0 if a tie
# If one of the values is not valid, that player loses. If bothnumbers are invalid, it is a tie.
def determineWinner():
if(player1Choice == 1):
if(player2Choice == 1):
return 0
elif(player2Choice == 2):
return 2
else:
return 1
elif(player1Choice == 2):
if(player2Choice == 1):
return 0;
elif(player2Choice == 2):
return 2
else:
return 1
elif(player1Choice == 3):
if(player2Choice == 1):
return 0
elif(player2Choice == 2):
return 2
else:
return 1
else: #something other than 1, 2, or 3
if(player2Choice >= 1 and player2Choice <= 3):
return 2
else:
return 0

# playGame function takes the number of rounds and plays thatmany rounds.
# It keeps track of the score, and returns the scores.
def playGame():
userScore = 0
compScore = 0
for turn in range(rounds):
winner = playRound()
if(winner==1):
compScore += 1
print(“–You win this round”)
elif(winner==2):
userScore += 1
print(“–Computer wins this round”)
else:
print(“–Tie this round”)
print()
return userScore, compScore

# playRound function calls the functions to get the user’s andthe computer’s selection.
# It returns the 0, 1, or 2 to determine tie, user win, or computerwin.
def playRound():
userChoice = userPick()
compChoice = userPick()
print(“You chose ” + displayPick(userChoice))
print(“The computer chose ” + displayPick())
return determineWinner(userChoice)
  
def main():
numRounds = int(input(“Enter the number of rounds: “))
while(numRounds < 1):
print(“Number must be positive.”)
numRounds = int(input(“Enter the number of rounds: “))
userScore = playGame(numRounds)
print(“Your score: {}nComputer score: {}”.format(compScore,userScore))
if(userScore < compScore):
print(“You Win!!”)
elif(compScore < userScore):
print(“Computer Wins!!”)
else:
print(“Tie Game”)

main()

Expert Answer


Answer to #IN PYTHON FIX the Program to run import random # userPick function gets a valid choice from the user and returns it def… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *