Hack #1 - Class Notes

Write any extra notes you have here:

  • Simulations are abstractions that mimic more complex objects or phenomena from the real world

Why a simulation is considered an abstraction:

  • 1: It uses a conditional to execute one part of the code only when a particular condition is met.
  • 2: It uses a REPEAT loop to run the same block of code multiple times.
  • 3: It simplifies a real-world scenario into something that can be modeled in code and executed on a computer.
  • 4: It does not request input from the user or display output to the user.
import random

x = random.randint(1, 100)
print(x)
82
def mycloset():
    myclothes = ["red shoes", "green pants", "tie", "belt"]
    print("Would you like to 'add' or 'trash' something from your closet?")
    user_choice = input()
    if user_choice == "add":
        new_clothes = ["sweater", "hat", "scarf", "jacket"]
        print(f"Adding {random.choice(new_clothes)} to your closet!")
        myclothes.append(random.choice(new_clothes))
    elif user_choice == "trash":
        print(f"Trashing {random.choice(myclothes)} from your closet!")
        myclothes.remove(random.choice(myclothes))
    else:
        print("Invalid input!")
    return myclothes

print(mycloset())
Would you like to 'add' or 'trash' something from your closet?
Trashing belt from your closet!
['red shoes', 'green pants', 'belt']
import random

num = random.random()
if num < 2/3:
  print("Heads")
else: 
  print("Tails")
Tails

Hack #2 - Functions Classwork

 

Hack #3 - Binary Simulation Problem

import random

def randomnum(): 
    

def converttobin(n): 
    

def survivors(binary): # function to assign position
    survivorstatus = ["Jiya", "Shruthi", "Noor", "Ananya" , "Peter Parker", "Andrew Garfield", "Tom Holland", "Tobey Maguire"]
def randomnum(): 
    randomnum = random.randint(0,255)
    return randomnum

def converttobin(n): 
    binary = bin(n)[2:]
    binary = binary.zfill(8)
    return binary

def survivors(binary): # function to assign position
    survivorstatus = ["Jiya", "Shruthi", "Noor", "Ananya" , "Peter Parker", "Andrew Garfield", "Tom Holland", "Tobey Maguire"]
    for i in range(8):
        if binary[i] == '1':
            print(survivorstatus[i] + " is a zombie!")
        else:
            print(survivorstatus[i] + " is safe!")

def main(): 
    n = randomnum()
    binary = converttobin(n) 
    survivors(binary) 

main()
Jiya is a zombie!
Shruthi is safe!
Noor is a zombie!
Ananya is safe!
Peter Parker is safe!
Andrew Garfield is safe!
Tom Holland is a zombie!
Tobey Maguire is safe!

Hack #4 - Thinking through a problem

  • create your own simulation involving a dice roll
  • should include randomization and a function for rolling + multiple trials
import random

def roll_dice(): 
    return random.randint(1, 6)

trials = int(input("How many trials would you like to do?: "))

for trial in range(trials):
    print("Trial", trial + 1, ":", roll_dice())
Trial 1 : 4
Trial 2 : 6
Trial 3 : 3
Trial 4 : 4
Trial 5 : 1
Trial 6 : 6

Hack 5 - Applying your knowledge to situation based problems

Using the questions bank below, create a quiz that presents the user a random question and calculates the user's score. You can use the template below or make your own. Making your own using a loop can give you extra points.

  1. A researcher gathers data about the effect of Advanced Placement®︎ classes on students' success in college and career, and develops a simulation to show how a sequence of AP classes affect a hypothetical student's pathway.Several school administrators are concerned that the simulation contains bias favoring high-income students, however.
    • answer options:
      1. The simulation is an abstraction and therefore cannot contain any bias
      2. The simulation may accidentally contain bias due to the exclusion of details.
      3. If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation.
      4. The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output.
  2. Jack is trying to plan his financial future using an online tool. The tool starts off by asking him to input details about his current finances and career. It then lets him choose different future scenarios, such as having children. For each scenario chosen, the tool does some calculations and outputs his projected savings at the ages of 35, 45, and 55.Would that be considered a simulation and why?
    • answer options
      1. No, it's not a simulation because it does not include a visualization of the results.
      2. No, it's not a simulation because it does not include all the details of his life history and the future financial environment.
      3. Yes, it's a simulation because it runs on a computer and includes both user input and computed output.
      4. Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.
  3. Sylvia is an industrial engineer working for a sporting goods company. She is developing a baseball bat that can hit balls with higher accuracy and asks their software engineering team to develop a simulation to verify the design.Which of the following details is most important to include in this simulation?
    • answer options
      1. Realistic sound effects based on the material of the baseball bat and the velocity of the hit
      2. A depiction of an audience in the stands with lifelike behavior in response to hit accuracy
      3. Accurate accounting for the effects of wind conditions on the movement of the ball
      4. A baseball field that is textured to differentiate between the grass and the dirt
  4. Ashlynn is an industrial engineer who is trying to design a safer parachute. She creates a computer simulation of the parachute opening at different heights and in different environmental conditions.What are advantages of running the simulation versus an actual experiment?
    • answer options
      1. The simulation will not contain any bias that favors one body type over another, while an experiment will be biased.
      2. The simulation can be run more safely than an actual experiment
      3. The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design.
      4. The simulation can test the parachute design in a wide range of environmental conditions that may be difficult to reliably reproduce in an experiment.
    • this question has 2 correct answers
  5. YOUR OWN QUESTION; can be situational, pseudo code based, or vocab/concept based
  6. YOUR OWN QUESTION; can be situational, pseudo code based, or vocab/concept based
import random

questions = {
  1: ["What does RAM stand for?", "Random Access Memory"],
  2: ["What is the most widely used sorting algorithm?", "Merge Sort"],
  3: ["What does TCP/IP stand for?", "Transmission Control Protocol / Internet Protocol"],
  4: ["What is the syntax for an if statement in Python?", "if (condition):"],
  5: ["What is the binary representation of the decimal number 15?", "1111"],
  6: ["What is the purpose of a compiler?", "To translate code written in a programming language into machine code"]
}

score = 0

for i in range(6):
  num = random.randint(1, 6)
  q = questions[num]
  print(q[0])
  user_answer = input("Type your answer: ")
  if user_answer == q[1]:
    print("Correct!")
    score += 1
  else:
    print("Incorrect!")

print("You scored", score, "out of 6!")
What is the binary representation of the decimal number 15?
Correct!
What does RAM stand for?
Correct!
What is the binary representation of the decimal number 15?
Correct!
What does TCP/IP stand for?
Correct!
What does TCP/IP stand for?
Correct!
What is the purpose of a compiler?
Correct!
You scored 6 out of 6!

Hack #6 / Challenge - Taking real life problems and implementing them into code

Create your own simulation based on your experiences/knowledge! Be creative! Think about instances in your own life, science, puzzles that can be made into simulations

Some ideas to get your brain running: A simulation that breeds two plants and tells you phenotypes of offspring, an adventure simulation...

import random 

#Create a list of possible outcomes
outcomes = ["win", "lose", "draw"]

#Create a function to run the simulation
def run_simulation():
    #Set the score to 0
    score = 0

    #Loop 10 times
    for _ in range(10):
        #Generate a random outcome
        outcome = random.choice(outcomes)

        #Add 1 to the score if the outcome is a win
        if outcome == "win":
            score += 1
    
    #Print the final score
    print("Final score: {}".format(score))

#Run the simulation
run_simulation()
Final score: 3