Question: Need help on Python Code. When running the program, the ouput is not producing the right result. My code is written down below. PYTHON CODE//
Need help on Python Code. When running the program, the ouput is not producing the right result.
My code is written down below.
PYTHON CODE//
import random
print("Let's Play The Game OF ROCK! PAPER! SCISSORS!") # The title of the game
print('------------------------')
# The instructions for the game & the # key values for each choice print('To pick ROCK, enter rock') print('To pick PAPER, enter paper') print('To pick SCISSORS, enter scissors')
print('------------------------')
# This function here will generate a random number # the computer will choose from def random_number(): rpsgame = random.randint(1, 3) return rpsgame
# This function is defined when the Computer # chooses 'Rock, Paper, & Scissors' against Player def computer_random_choice(rpsgame): computer_rps = "" if rpsgame == 1: computer_rps = "Rock"
elif rpsgame == 2: computer_rps = "Paper"
elif rpsgame == 3: computer_rps = "Scissors"
return computer_rps
# This function is defined when the User # chooses 'Rock, Paper, & Scissors' against Computer def player_random_choice(): player_rps = input('Enter the following choices, Rock, Paper, or Scissors:') return player_rps
# This function defines the possibilities of choosing # 'Rock' 'Paper' & 'Scissors' when the User plays against the computer def computerplayer_game(player_rps, computer_rps): # Rock & Paper Combinations if player_rps == "Rock" and computer_rps == "Paper": print('Winner: Computer / Loser: Player') print('You Lose. Sorry Try Again')
elif player_rps == "Paper" and computer_rps == "Rock": print('Winner: Player / Loser: Computer') print('Congratulations!!! You Won The Game')
# Scissors & Rock Combinations elif player_rps == "Scissors" and computer_rps == "Rock": print('Winner: Computer / Loser: Player') print('You Lose. Sorry Try Again')
elif player_rps == "Rock" and computer_rps == "Scissors": print('Winner: Player / Loser: Computer') print('Congratulations!!! You Won The Game')
# Paper & Scissors Combinations elif player_rps == "Paper" and computer_rps == "Scissors": print('Winner: Computer / Loser: Player') print('You Lose. Sorry Try Again')
elif player_rps == "Scissors" and computer_rps == "Paper": print('Winner: Player / Loser: Computer') print('Congratulations!!! You Won The Game')
def main(): player_rps = player_random_choice() # Validate player input, if enters invalid value, ask to enter again while player_rps != "Rock" and player_rps != "Paper" and player_rps != "Scissors": player_rps = player_random_choice()
rpsgame = random_number() computer_rps = computer_random_choice(rpsgame)
# Print computer's choice print('Computer''s choice: ', computer_rps)
# If computer and player's choices are the same # Print message and start game again till their values are not same # so that winner can be determined while ((player_rps == "Rock" and computer_rps == "Rock") or (player_rps == "Paper" and computer_rps == "Paper") or (player_rps == "Scissors" and computer_rps == "Scissors")): print('You Both Tied. Play Again') print() player_rps = player_random_choice() rpsgame = random_number() computer_rps = computer_random_choice(rpsgame) print('Computer''s choice: ', computer_rps)
# Call function to find who is winner # This function will be called, after computer and player have different choices computerplayer_game(player_rps, computer_rps)
main()
OUPUT//
Let's Play The Game OF ROCK! PAPER! SCISSORS! ------------------------ To pick ROCK, enter rock To pick PAPER, enter paper To pick SCISSORS, enter scissors ------------------------ Enter the following choices, Rock, Paper, or Scissors: Rock Enter the following choices, Rock, Paper, or Scissors: Paper Enter the following choices, Rock, Paper, or Scissors: Scissors Enter the following choices, Rock, Paper, or Scissors: rock Enter the following choices, Rock, Paper, or Scissors: 'paper Enter the following choices, Rock, Paper, or Scissors: 'rock' Enter the following choices, Rock, Paper, or Scissors: scissors Enter the following choices, Rock, Paper, or Scissors:
Its keeps repeating the user input, and not the computer's choice and result of the game.
Really need help on this thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
