Check for the Winner
Learn how to check for the winner of the game.
We'll cover the following
What have we got?
We have collected all the data required to play the game. This is our inventory of already created functions:
# Import command for the 'random' library
import random
# Function to display game instructions
def display_instructions():
print('Welcome to Rock Paper Scissors.\n')
print('The rules are simple:')
print('1. Rock beats scissors')
print('2. Scissors beat paper')
print('3. Paper beats rock')
# Function to ask for the number of rounds
def get_rounds():
rounds = input('\nHow many rounds would you like to play? \nRounds: ')
rounds = int(rounds)
return rounds
# Function to get the player's choice
def get_player_choice():
player_choice = input('\nPlease enter rock, paper, or scissors. \nChoice: ')
return player_choice
# Function to get the computer's choice
def get_computer_choice():
computer_choice = random.choice(['rock', 'paper', 'scissors'])
print('Computer chooses', computer_choice)
return computer_choice
Considering our code so far, we still lack the ability to determine the winner of a round.
What’s next?
We can create a function that compares players’ choices and returns the name of the round winner. In the main()
function, we can check the name of the winner and update the scores accordingly.
Try it yourself
Write your code in the code widget below and click the “Run” button to execute your code.
If you’re unsure how to do this, click the “Show Hint” button.
Get hands-on with 1400+ tech skills courses.