Hack #1 - Class Notes

  • Simulations are abstractions that mimic more complex objects or phenomena from the real world
  • Purpose: to draw inferences about things we cant obsevre
  • Variability and randomness in the world can be the reason behind bias in simulations
  • Ex: rolling dice, chemical reactions, molecular models, etc
import random
n = int(input("What is the range?"))
random.randint(0,n)
86
def mycloset():
    myclothes = ['red shoes', 'green pants', 'tie', 'belt']
    x = random.choice(myclothes)
    myclothes.remove(x)
    print(myclothes)

mycloset()
['green pants', 'tie', 'belt']

Hack #2 - Functions Classwork

import random

def coinflip():         #def function 
    randomflip = random.randint(0, 2) #picks either 0 or 1 randomly (50/50 chance of either) 
    if randomflip == 0: #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    elif randomflip == 1: #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 2: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
            print("Tails")

#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()

# Created coin flip in another way using lists
def coinfliplist():
    list = ['Heads', 'Tails', 'Heads']
    choice = random.choice(list)
    if choice == 'Heads':
        print('Heads')
    else: 
        print('Tails')
print(' ')
print(' ')
coinfliplist()
coinfliplist()
coinfliplist()
Tails
Heads
Tails
 
 
Tails
Heads
Heads

Hack #3 - Binary Simulation Problem

import random

def randomnum(): # function for generating random int
    y = random.randint(0,255)
    print(y)
    return int(y)



def convert(num):
    return bin(num).replace("0b", "")

    

def survivors(y): # function to assign position
    survivorstatus = ["Jiya", "Shruthi", "Noor", "Ananya" , "Peter Parker", "Andrew Garfield", "Tom Holland", "Tobey Maguire"]
    # replace the names above with your choice of people in the house
    list = []
    list = [*y]
    dict = {}
    for key in survivorstatus:
        for value in list:
            dict[key] = value
            list.remove(value)
    print(dict)
      #  if (list[i] == 0):
      #      print(survivorstatus[i] + "is a zombie")
       # else: 
         #   print(survivorstatus[i] + "is a survivor")
    



x = randomnum()
y = convert(x)
survivors(y)
245
{'Jiya': '0', 'Shruthi': '1', 'Noor': '1', 'Ananya': '1'}
word = "Hello"
list = []
list = [*word]
print(list)
['H', 'e', 'l', 'l', 'o']

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
# Two in a row game

def rolldice():
    x = random.randint(1,6)
    return(x)
    

def points():
    points = 0
    list = []
    y = rolldice()
    z = y
    list.append(y)
    y = rolldice()
    print("You rolled:", z, " and ", y)
    if z == y:
        print("You got two in a row, you get one point")
        points = points + 1

points()
You rolled: 6  and  6
You got two in a row, you get one point

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
questions = 6
correct = 0


qa = {
    "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. a) The simulation is an abstraction and therefore cannot contain any bias b) The simulation may accidentally contain bias due to the exclusion of details c) If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation d) The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output": "b",
    "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? a) No, it's not a simulation because it does not include a visualization of the results. b) No, it's not a simulation because it does not include all the details of his life history and the future financial environment c) Yes, it's a simulation because it runs on a computer and includes both user input and computed output. d) Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.": "c",
    "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? a) Realistic sound effects based on the material of the baseball bat and the velocity of the hit b) A depiction of an audience in the stands with lifelike behavior in response to hit accuracy c) Accurate accounting for the effects of wind conditions on the movement of the ball d) A baseball field that is textured to differentiate between the grass and the dirt": "c",
    "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? a) The simulation will not contain any bias that favors one body type over another, while an experiment will be biased b) The simulation can be run more safely than an actual experiment c) The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design d) The simulation can't test the parachute design in a wide range of environmental conditions that may be difficult to reliably reproduce in an experiment": "b",
    "True or False: Simulations can be biased, a) True b) False": "a",
    "True or False: Simulations are abstractions that mimic more complex objects or phenomena from the real world, a) True b) False": "a",
}

def prompt(q, a):
    answer = input(q + ' : choose from the following answer choices')
    if answer == a:
        print('correct!')
        return 1
    else:
        print('incorrect!')
        return 0

for i in qa:
    y = prompt(i, qa[i])
    correct = correct + y

percent =  100* correct/questions




print('You got ', str(correct), ' out of ', str(questions))
print('That is' ,percent, '%')
if percent < 50:
    print('You failed!')
else: 
    print('You passed')
correct!
correct!
correct!
correct!
correct!
incorrect!
You got  5  out of  6
That is 83.33333333333333 %
You passed

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
money = 0
slot = [1, 2, 3, 4, 5, 6, 7, 8, 9]
choice = []
mlist = []

def check():
    money = 0
    for i in range (3):
        x = random.choice(slot)
        choice.append(x)
    print('Slot:' , choice)
    if choice[0] == choice[1]:
        money = money + 5
        print('+5k')
    elif choice[0] == choice[2]:
        money = money + 5
        print('+5k')
    elif choice[1] == choice[2]:
        money = money + 5
        print('+5k')
    else: 
        money = money - 5
        print('-5k')
    mlist.append(money)
    return money 
    

rep = int(input("How many times would you like to run the slot machine? Remember, no repeats means you pay 5k. Repeats mean you gain 5k."))
total = 0
for i in range(rep):
    check() 
    choice.clear()


for i in mlist: 
    total = total + i

print('')
print('Your total money: $', total, 'k')
if total < 0:
    print('You are in debt, maybe the slot machine isnt for you')
elif total == 0: 
    print("No loss, no gain. Try your luck again?")
else:
    print('Hey, way to go! Youre pretty lucky today, go and buy a lottery ticket')


        
Slot: [9, 8, 2]
-5k
Slot: [2, 2, 6]
+5k
Slot: [7, 1, 5]
-5k
Slot: [8, 1, 7]
-5k

Your total money: $ -10 k
You are in debt, maybe the slot machine isnt for you