# Initialize Otter
import otter
grader = otter.Notebook("lab6-RPS.ipynb")

Lab 6: Rock, Paper, Scissors Game#

In this lab you will use branching to complete a playable game of rock paper scissors.

Entering Your Information for Credit#

To receive credit for assignments it is important we can identify your work from others. To do this we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

# Please provide your first name, last name, Drexel ID, and Drexel email. Make sure these are provided as strings. "STRINGS ARE TEXT ENCLOSED IN QUOTATION MARKS."

# In the assignments you will see sections of code that you need to fill in that are marked with ... (three dots). Replace the ... with your code.
first_name = ...
last_name = ...
drexel_id = ...
drexel_email = ...
grader.check("q0-Checking-Your-Name")
import random

History of Rock, Paper, Scissors#

Rock paper scissors (also known by other orderings of the three items, with β€œrock” sometimes being called β€œstone,” or as Rochambeau, roshambo, or ro-sham-bo) is a hand game originating from China, usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are β€œrock” (a closed fist), β€œpaper” (a flat hand), and β€œscissors” (a fist with the index finger and middle finger extended, forming a V). β€œScissors” is identical to the two-fingered V sign (also indicating β€œvictory” or β€œpeace”) except that it is pointed horizontally instead of being held upright in the air.

A simultaneous, zero-sum game, each round has three possible outcomes: a tie, a win or a loss. A player who decides to play rock will beat another player who has chosen scissors (β€œrock crushes scissors” or β€œbreaks scissors” or sometimes β€œblunts scissors”), but will lose to one who has played paper (β€œpaper covers rock”); a play of paper will lose to a play of scissors (β€œscissors cuts paper”). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie. The game spread from China while developing different variants in signs over time.

Task 1: Check for valid input

In your game of Rock, Paper, Scissors, it will be the player vs. the computer. The player will make their selection each round by typing either "rock", "paper", or "scissors". The first task is to write a function that checks to make sure that the input entered by the player is valid.

Write python code to do the following:

  • Complete the function called valid_input which takes as input a string player_choice.

  • The function should return True if player_choice equals "rock", "paper", or "scissors".

  • Otherwise the function should return False.

Your code replaces the prompt: ...

def valid_input(player_choice):
    ...

# use this to check your results
valid_input("paper")
grader.check("task1-input")

Task 2: Get Round Result Message

In each round, we want to display a result message that explains the outcome. Your next task is to write a function to determine the message that should displayed based on the choices of the player and computer. The table below shows the message to display if the player and computer choose different options.

player_choice

computer_choice

message

"rock"

"paper"

"You lose! Paper covers rock."

"rock"

"scissors"

"You win! Rock crushes scissors."

"paper"

"rock"

"You win! Paper covers rock."

"paper"

"scissors"

"You lose! Scissors cuts paper."

"scissors"

"rock"

"You lose! Rock crushes scissors."

"scissors"

"paper"

"You win! Scissors cuts paper."

If the player and the computer make the same choice, the the displayed message should be: "It's a tie!"

Write python code to do the following:

  • Complete the function called round_message which takes in two strings, player_choice and computer_choice.

  • If the player and computer choose different options, the function should return a string containing the appropriate message based on the table above.

  • If the player and computer choose the same, then the tie message above should be returned.

Your code replaces the prompt: ...

def round_message(player_choice, computer_choice):
    ...

# use this to check your results
round_message("paper", "scissors")
grader.check("task2-round-message")

Task 3: Get Final Message

The game will be played in rounds until one player has reached a score of 2. When the game ends, a final message is displayed indicating whether the player won or lost. Your final task is to write the function that determines this final message based on the score.

Write python code to do the following:

  • Complete the function called final_message which takes in two integers, player_score and computer_score.

  • If the player_score is greater than computer_score, return the string: "You won the game!"

  • If the player_score is less than computer_score, return the string: "You lost the game!"

  • You don’t need to worry about handling a tie, since the game will only end when someone has won.

Your code replaces the prompt: ...

def final_message(player_score, computer_score):
    ...

# use this to check your results
final_message(2,0)
grader.check("task3-final-message")

Play the game!#

Once you’ve completed the three tasks above, run the code below to play the game. Read through the commented code carefully and try to follow the logic of the implementation.

# imports randomint package to select a random integer
from random import randint

# a list of choices the compute can pick from
choices = ["rock", "paper", "scissors"]

# initialize the score to 0/0
player_score = 0
computer_score = 0

# initialize the round number to 1
round = 1

# keep playing while nobody has a score == 2
while player_score < 2 and computer_score < 2:

    # print the round number
    print(f"Round {round}\n-------")

    # loop forever! (until we break out)
    while True:
        # get input from the player
        player_choice = input("Your choice:")
        # if the input is valid, break out of the while loop
        if valid_input(player_choice):
            break
        # otherwise warn the player of invalid input and have them try again
        else:
            print("Invalid input. Must be \"rock\", \"paper\" or \"scissors\". Try again.")
    
    # have a computer make a random selection from the choices
    computer_choice = choices[randint(0,2)]

    # print out the player and computer choices
    print(f"Player choice: {player_choice}")
    print(f"Computer choice: {computer_choice}")

    # get and store the round result message
    message = round_message(player_choice, computer_choice)

    # print the message
    print(message)

    # use the round result message to tell who won
    # split the message at the spaces to get a list of words
    # The second word (at index 1) is "win!" or "lose!" indicating the result
    if message.split()[1] == "win!":
        # add 1 to the player score
        player_score += 1
    elif message.split()[1] == "lose!":
        # add 1 to the computer score
        computer_score += 1

    # increment the round number
    round += 1
    # print an empty line
    print()

# get and print the final message
print(final_message(player_score, computer_score))
# print the score
print(f"Final score | You : {player_score} | Computer : {computer_score} |")

Submitting Your Assignment#

To submit your assignment please use the following link the assignment on GitHub classroom.

Use this link to navigate to the assignment on GitHub classroom.

If you need further instructions on submitting your assignment please look at Lab 1.

Viewing your score#

It is your responsibility to ensure that your grade report shows correctly. We can only provide corrections to grades if a grading error is determined. If you do not receive a grade report your grade has not been recorded. It is your responsibility either resubmit the assignment correctly or contact the instructors before the assignment due date.

Each .ipynb file you have uploaded will have a file with the name of your file + Grade_Report.md. You can view this file by clicking on the file name. This will show you the results of the autograder.

We have both public and hidden tests. You will be able to see the score of both tests, but not the specific details of why the test passed or failed.

Note

In python and particularly jupyter notebooks it is common that during testing you run cells in a different order, or run cells and modify them. This can cause there to be local variables needed for your solution that would not be recreated on running your code again from scratch. Your assignment will be graded based on running your code from scratch. This means before you submit your assignment you should restart the kernel and run all cells. You can do this by clicking Kernel and selecting Restart and Run All. If you code does not run as expected after restarting the kernel and running all cells it means you have an error in your code.

Fin#