<jupyter_start><jupyter_text>Now You Code In Class : Rock Paper Scissors ExperimentIn this now you code we will learn to re-factor a program into a function. This is the most common way to write a function when you are a beginner. *Re-factoring* is the act of re-writing code without changing its functionality. We commonly do re-factoring to improve performance or readability of our code. Through the process we will also demonstrate the DRY (don't repeat yourself principle of coding).The way you do this is rather simple. First you write a program to solve the problem, then you re-write that program as a function and finally test the function to make sure it works as expected. This helps train you to think abstractly about problems, but leverages what you understand currently about programming. Introducing the Write - Refactor - Test - Rewrite approachThe best way to get good at writing functions, a skill you will need to master to become a respectable programmer, is to use the **Write - Refactor - Test - Rewrite** approach.  The basic idea is as follows:1. Write the program 2. Identify which parts of the program can be placed in a function3. Refactor the code into a function. Extract the bits into a function into a new, separate cell independent of the original code. 4. Test the function so you are confident it works, using the expect... actual approach from the lab.5. Re-Write the original program to call the function instead. The Problem Which Rock, Paper Scissors strategy is better? Random guessing or always choosing one of rock, paper, or scissors?Let's write a program which plays the game of Rock, Paper, Scissors 10,000 times and compares strategies.  The Approach1. Write the program once (done for you in the cell below)2. refactor step 1 into its own function `play_game(playera, playerb)` which returns the winning player.3. test the function to make sure it works. Write tests for all cases4. re-write the main program to now call the function.5. use the function in the final program.<jupyter_code>#ORIGINAL CODE

import random 
choices = ['rock', 'paper', 'scissors']
wins = 0
losses = 0
ties = 0

computer = random.choice(choices)
you = 'rock' #Always rock strategy

if (you == 'rock' and computer == 'scissors'):
    outcome = "win"
elif (you == 'scissors' and computer =='rock'):
    outcome = "lose"
elif (you == 'paper' and computer =='rock'):
    outcome = "win"
elif (you == 'rock' and computer=='paper'):
    outcome = "lose"
elif (you == 'scissors' and computer == 'paper'):
    outcome = "win"
elif (you == 'paper' and computer == 'scissors'):
    outcome = "lose"
else:
    outcome = "tie"

print(f"You:'{you}' Computer:'{computer}' Game: {outcome} ")<jupyter_output><empty_output><jupyter_text>Problem AnalysisFor a function `rock_paper_scissors()` which plays the game, what are the inputs and outputs?Inputs:  PROMPT 1Outputs:  PROMPT 2Function def in python:   (just `def` part)PROMPT 3<jupyter_code># PROMPT 4: Write function<jupyter_output><empty_output><jupyter_text>Test CasesWriting a function is not helpful unless we have some assurances that it is correct. We solve this problem with test cases:    YOU      COMPUTER  OUTCOME    Rock     Rock      Tie    Rock     Scissors  Win    Rock     Paper     Lose        Scissors Rock      Lose    Scissors Scissors  Tie    Scissors Paper     Win        Paper    Rock      Win    Paper    Scissors  Lose    Paper    Paper     Tie    PROMPTS 5 - 13   Write a `print()` statement for each test case:`When YOU=?, ROCK=?, EXPECT=?, ACTUAL=(call the function)`<jupyter_code># PROMPTS 5-13 test Cases<jupyter_output><empty_output><jupyter_text>Re-WriteWith the function code tested, and assurances it is correct, we can now re-write the original program, calling the function instead.<jupyter_code>import random 
choices = ['rock', 'paper', 'scissors']
computer = random.choice(choices)
you = 'rock'   

# TODO: PROMPT 14 call function
    
print(f"You:'{you}' Computer:'{computer}' Game: {outcome} ")<jupyter_output><empty_output><jupyter_text>Back to the simulationNow that we have function to play the game, we can build our simulation. Remember the original problem was to compare strategies by playing the game thousands of times. There are 3 strategies for the user:- Rock (always choose rock)- Paper (always choose paper)- Scissors (always choose scissors)- Random (choose one at random)INPUTS:- strategy- number of times to play the gameOUTPUTS: Number of:- Wins- Losses- TiesALGORITHM:    PROMPT 15     Final Code Let's use ipywidgets to create the user interface. Some notes:- the list of items `["rock","paper","scissors","random"]` makes a drop down.- `times=(10_000, 100_000, 10_000)` produces a slider with min 10,000, max 100,000 and increment of 10,000<jupyter_code>from IPython.display import display, HTML
from ipywidgets import interact_manual
import random 

@interact_manual(strategy=["rock","paper","scissors","random", "pick2"], times=(10_000, 500_000, 10_000) )
def main(strategy, times):
    display(f"Running simulation {times} times using strategy {strategy}.")
    # TODO Implement algorithm as code
    

# run this code to turn in your work!
from coursetools.submission import Submission
Submission().submit_now()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08273244781783681, 'fraction_numerical': 0.015749525616698293, 'mean_word_length': 4.41726618705036, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '355117', 'license': 'MIT', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1442, 'n_words': 693, 'path': 'content/lessons/05-Functions/SmallGroup-Functions.ipynb', 'repo_name': 'IST256/learn-python', 'size': '5270'}