Joining the Dots

Learn how to set up the main function using all functions.

What have we got?

We have catered to all the components required to play a round. All the essential functionalities are there, how will we implement the code for multiple rounds? 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():
    options = ['rock', 'paper', 'scissors']
    computer_choice = random.choice(options)
    print('Computer chooses', computer_choice)
    return computer_choice

# Function to determine the winner of the round
def determine_round_winner(player_choice, computer_choice):
    if player_choice == computer_choice:
        print('Tie!')
    elif player_choice == 'rock' and computer_choice == 'scissors':
        print('You win!')
        return 'player'
    elif player_choice == 'rock' and computer_choice == 'paper':
        print('Computer wins!')
        return 'computer'
    elif player_choice == 'paper' and computer_choice == 'rock':
        print('You win!')
        return 'player'
    elif player_choice == 'paper' and computer_choice == 'scissors':
        print('Computer wins!')
        return 'computer'
    elif player_choice == 'scissors' and computer_choice == 'rock':
        print('Computer wins!')
        return 'computer'
    elif player_choice == 'scissors' and computer_choice == 'paper':
        print('You win!')
        return 'player'

# Function to contain the flow of the whole program
def main():
    display_instructions()
    rounds = get_rounds()
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()
    winner = determine_round_winner(player_choice, computer_choice)
    if winner == 'player':
        player_score += 1
    elif winner == 'computer':
        computer_score += 1
    else:
        pass
    print('Player score:', player_score, '| Computer score:', computer_score)

What’s next?

In the main() function, which manages the game’s flow, we will make the steps required for each round go in a loop. We need to run these lines for the total number of rounds using the for loop.

The portion of code that will need to go in the loop includes:

  • The get_player_choice() function
  • The get_computer_choice() function
  • The determine_round_winner() function
  • The portion to update and print the score

Let’s implement a for loop for the number saved in round, and call these functions in the loop.

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.