Homework 2-4

Due: March 6, 11:59 pm

Rock-Paper-Scissors

Introduction

In this homework, you will be implementing the game Rock-Paper-Scissors. In Task 1, you will ask for and filter the user's choice. In Task 2, you will randomly generate the computer's choice and then determine who won the game.

This is an example of what your program should output during one run where the user inputs "rock".

Rock-Paper-Scissors

Enter your choice: rock

Computer chose scissors

You win!

You should use ACT2-4 as a guideline for approaching each section of this assignment.

Task 1

First, you will ask for user input and filter it by extracting the keyword "rock", "paper", or "scissors". You should consider input that contains more than one of these keywords to be invalid input, printing out an error message and exiting the program with the exit() function.

Think about what you will need to do to ensure that differences in capitalization are taken into account and accepted, as well as keywords surrounded by other words and characters. For instance, "rock", "Rock!!", and "I choose ROCK" are all valid, but "rock scissors" is not.


Task 2

To generate a random choice for the computer, you will use Python's random number module. You will learn about modules in more detail in a later lecture, but for now, just know that modules are sections of pre-written code that you can include in your own program using the import keyword.

To use Python's random number module, add the line import random to the beginning of your program.

To generate a random computer choice, use the code: random.choice(["rock", "paper", "scissors"]). This calls the choice() function within the random module, passing in a list (represented by the brackets) of the possible options. You will learn more about lists in an upcoming lecture.

After you have generated a random choice for the computer and stored it in a variable, you will compare it with the user's choice and determine a winner. You will then print out a message with the outcome.

Task 3

Please put the number of hours worked, any collaborators and whether you went to TA hours in comments at the top of your python file


Handin

Once you're done, share your file with cs0030handin@gmail.com by midnight, 3/6.

Make sure your submission has your name in the filename: FirstLast_HW2-4.py. “FirstLast” should be replaced with your first and last name or we will take off points. Make sure every task has been completed.


Extra Credit

If you would like to do extra credit, please explain what you did in a separate file named FirstLast_HW2-4_README.txt. Note that extra credit is worth up to 10% of the grade for this homework and is awarded at the discretion of the TAs.

Here are some suggestions:

  • Instead of directly comparing the user's choice with the computer's, you can use modular arithmetic. In Python, % is the modulo operator. In essence, this method converts the choices to numbers and compares the numbers to determine the winner, taking into account the circularity of the outcome determination. You can read more about it here.

  • You can filter the user input more strictly by considering inputs that contain other words to be invalid. This means that inputs like "I choose rock" are invalid but inputs like "rock!" are still valid.

  • Rock-Paper-Scissors is a classic but boring game. You know what will make it more fun? Rock-Paper-Scissors-Lizard-Spock.