# Assignment 1

Put all your answers in one CodeSkulptor file. Use Python comments to include any answers that are not Python code. Use Python comments to clearly mark each problem number.

Work in assigned pairs on this assignment. Submit it on OWL-Space, under Assignment 1. Only the FIRST person listed in the pair should submit the Codeskulptor URL. Put the pair designation as posted, (NetID1, NetID2), at the top of your submission as well as in Python comments at the top of your CodeSkulptor code.   Including the person's actual names too would be nice.

As a matter of good style and conciseness, wherever reasonable you should use functions that have been defined previously on this assignment, on a previous assignment, or in class.

Be sure to read the course policies.

## We're going to the movies! (30 points total)

Our friend owns a small one-screen movie theater and recently ran an experiement to determine the relationship between the price of a ticket and attendance. At a price of \$10.00 per ticket, 200 people attend a performance. For each decrease in price by a quarter (\$.25), the attendance increases by 15. Unfortunately, the increased attendance also comes at an increased cost in royalties and overhead. Every performance costs the owner \$1000, plus five cents (\$0.05) per attendee.

The owner would like to know the exact relationship between profit and ticket price so that he can determine the price at which he can make the highest profit.

While the task is clear, how to go about it is not. All we can say at this point is that several quantities depend on each other. When we are confronted with such a situation, it is best to tease out the various dependencies one at a time:

• Profit is the difference between revenue and cost.
• The revenue is exclusively generated by the sale of tickets. It is the product of the ticket price and the number of attendees.
• The costs consist of two parts: a fixed part (\$1000) and a variable part that depends on the number of attendees.
• Finally, the problem statement also specifies how the number of attendees depends on the ticket price.
1. (5 points)

Calculate the following by hand. How many attendees can afford a show at a ticket price of \$6.00, \$8.00, and \$10.00? What are the total costs, revenue, and profit for each show at these same prices?

2. (5 points)

Define a mathematical equation for each of the following: the number of attendees, the costs, revenue, and profit. Your equations should refer each other where possible, without being circular.

3. (20 points)

Define the corresponding Python functions: `attendees(ticket_price)`, `costs(ticket_price)`, `revenue(ticket_price)`, and `profit(ticket_price)`. Again, your functions should call each other where possible, without being circular.

You do not need to check for erroneous input values in these functions.

## Rock-Paper-Scissors-Lizard-Spock (70 points total)

Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw" one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:

• Rock smashes scissors
• Scissors cuts paper
• Paper covers rock

Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details). Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties more unlikely.

Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS's popularity is that it has been featured in 3 episodes of the TV series "The Big Bang Theory". The Wikipedia entry for RPSLS gives the complete description of the details of the game.

You will build a Python function `rpsls(guess)` that takes as input the string `guess`, which is one of `"rock"`, `"paper"`, `"scissors"`, `"lizard"`, or `"Spock"`. The function then simulates playing a round of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule that I will next describe.

While Rock-paper-scissor-lizard-Spock has a set of ten rules that logically determine who wins a round of RPSLS, coding up these rules would require a large number (5×5=25) of `if`/`elif`/`else` clauses in your code. A simpler method for determining the winner is to assign each of the five choices a number:

• 0 — rock
• 1 — Spock
• 2 — paper
• 3 — lizard
• 4 — scissors

In this expanded list, each choice wins agains the preceding two choices and loses against the following two choices. We have provided a video walk-through of the steps involved in building your code to aid its development. A template for your code is available here. Please work from this template.

Finally, if a player's guess is not one of the five acceptable values, your program should report that.

### Development process

1. Build a helper function `name_to_number(name)` that converts the string `name` into a number between -1 and 4 — 0 through 4 as described above, plus -1 if the string is not one of the valid values. This function should use an `if`/`elif`/`else` with 6 cases. You can use conditions of the form `name == 'paper'`, etc., to distinguish the cases.
2. Next, you should build a second helper function `number_to_name(num)` that converts a number in the range 0 to 4 into its corresponding guess as a string.
3. Build the first part of the main function `rpsls(guess)` that converts `guess` into the number `player_number` between -1 and 4 using the helper function `name_to_number`. It also prints an error message if that number is -1.
4. Build the second part of `rpsls(guess)` that generates a random number `comp_number` between 0 and 4 using the function `random.randrange()`. I suggest experimenting with `randrange` in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range.
5. Build the last part of `rpsls(guess)` that determines and prints out the winner. This test is actually very simple if you apply modular arithmetic (`%` in Python) to the difference between `comp_number` and `player_number`. If this is not immediately obvious to you, I would suggest reviewing our discussion of remainder / modular arithmetic and experimenting with the remainder operator `%` in a separate CodeSkulptor window to understand its behavior.
6. Using the helper function `number_to_name`, you should produce four `print` statements; print a blank line, print out the player's choice, print out the computer's choice and print out the winner.

You will simply call your `rpsls` function repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you are testing, feel free to modify those calls, but make sure they are restored when you hand in your assignment.

The output of running your program should have the following form:

```Player chooses rock.
Computer chooses scissors.
Player wins!

Player chooses Spock.
Computer chooses lizard.
Computer wins!

Player chooses paper.
Computer chooses lizard.
Computer wins!

Player chooses lizard.
Computer chooses scissors.
Computer wins!

Player chooses scissors.
Computer chooses Spock.
Computer wins!

Player chooses rocket.
Improper player choice.
```

To guide you in determining whether your assignment satisfies each item in the rubric, please consult the video that demonstrates our implementation of "Rock-paper-scissors-lizard-Spock". Small deviations from the textual output of our implementation are fine. You should avoid large deviations (such as using the Python function `input` to input your guesses). We've added error-checking to what was described in the video.

Here is a break down of the scoring:

• (8 points)

Program implements the function `rpsls()` and the helper functions `name_to_number()` and `number_to_name` with plausible code.

Partial credit of 4 points if only the function `rpsls()` has plausible code.

• (5 points)

Running program does not throw an error.

• (2 points)

Program prints a blank line between games.

• (8 points)

Program prints `"Player chooses player_guess"` where `player_guess` is a string of the form `"rock"`, `"paper"`, `"scissors"`, `"lizard"`, or `"Spock"`.

Partial credit of 4 points if program prints out a number instead of a string.

• (8 points)

Program prints `"Computer chooses computer_guess"` where `computer_guess` is a string of the form `"rock"`, `"paper"`, `"scissors"`, `"lizard"`, or `"Spock"`.

Partial credit of 4 points if program prints out a number instead of a string.

• (4 points)

Computer's guesses vary between five calls to `rpsls()` in each run of the program.

• (4 points)

Computer's guesses vary between runs of the program.

• (16 points)

Program prints `"Player and computer tie!"`, `"Player wins!"`, `"Computer wins!"`, or `"Improper player choice."` to report outcome. (4 points per message.)

• (15 points)

Program chooses correct winner according to RPSLS rules. Five cases correct: 15 points; four cases correct: 12 points; three cases correct: 8 points one or two cases correct: 4 points; no cases correct: 0 points.

You are not expected to use Python lists on this assignment, but you may.