Hack #1 - Class Notes

  • Simulations mimic the real world, things we cannot do in real life
  • Reflect the changing state of a phenomenon
  • Its impossible to factor everything into a simulation(the results may be a bit skewed)
  • The importance of simulaitons is to have test cases
  • Account for the importance of the simulations ideas
  • Random gives random
import random
#The below 2 inputs get the maximum and the minimum
min = int(input("WHat is lowest num"))
max = int(input("HIghest num plz"))
#Sets x to a random value in between the min and maximum
x = random.randint(min,max)
print(x)
8

Hack #2 - Functions Classwork

import random
#The two user inputs ask what items the user wants to change from the myclothes list
add = input("What do you want to add")
remove = input("What you to remove")
def mycloset():
    #List of clothes in the closet
    myclothes = ["red shoes", "green pants", "tie", "belt"]
    #The below lines add or remove the item from the users closet
    myclothes.append(add)
    myclothes.remove(remove)
    #Prints a random clothing item from the list
    print(random.choice(myclothes))
mycloset()
jordans
import random

def coinflip():         #def function 
    randomflip = random.randint(1,3) #picks either 0 or 1 randomly (50/50 chance of either) 
    if randomflip == 2 or randomflip ==1 : #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 3: #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()
t4 = coinflip()
t5 = coinflip()
Heads
Tails
Tails
Heads
Heads

Hack #3 - Binary Simulation Problem

import random

def randomnum(): # function for generating random int
    integer = random.randint(0,255)
    return integer
def converttobin(n): # function for converting decimal to binary
  binary = str(bin(n)[2:].zfill(8))


def survivors(binary): # function to assign position
    survivorstatus = ["Jalen", "AJ", "Miles", "Sanders" , "Jordan", "Lane", "Jason", "Darius"]
    i = 0
    for bit in binary:
        if bit == "0":
            print(survivorstatus[i], "is a zombie")
        if bit == "1":
            print(print(survivorstatus[i], "is a safe"))


survivors(converttobin(randomnum()))
    # replace the names above with your choice of people in the house
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb Cell 8 in <cell line: 22>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=17'>18</a>         if bit == "1":
     <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=18'>19</a>             print(print(survivorstatus[i], "is a safe"))
---> <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=21'>22</a> survivors(converttobin(randomnum()))

/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb Cell 8 in survivors(binary)
     <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a> survivorstatus = ["Jalen", "AJ", "Miles", "Sanders" , "Jordan", "Lane", "Jason", "Darius"]
     <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a> i = 0
---> <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> for bit in binary:
     <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a>     if bit == "0":
     <a href='vscode-notebook-cell://wsl%2Bubuntu-22.04/home/prasithchilla/Fastpages/_notebooks/2022-12-12-hw.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a>         print(survivorstatus[i], "is a zombie")

TypeError: 'NoneType' object is not iterable

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
#Asks for user input about how many dice rolls they want
n = int(input("How many dice rolls?"))
#Function roll
def roll(n):
    #Cycles through how many rolls they want
    for i in range (n):
        #Rolls the dice (getting a random number between 1 and 6)
        x = random.randint(1,6)
        #prints the roll of the dice
        print("Your dice roll is", x)
roll(n)
Your dice roll is 4
Your dice roll is 4
Your dice roll is 1
Your dice roll is 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

def quiz():
  # Create a list of questions
  questions = [
    "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.",
    "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?",
    "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?",
    "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?",
    "What is the use of simulations?",
    "What are problems we encounter with simulations"
  ]

  # Create a list of answers
  answers = [
    "The simulation may accidentally contain bias due to the exclusion of details",
    "Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.",
    "Thomas Edison",
    "H",
    "Mount Everest",
    ""
  ]

  # Create a list of multiple choice options for each question
  options = [
    ["Paris", "London", "Rome", "Madrid"],
    ["Saturn", "Jupiter", "Mars", "Venus"],
    ["Thomas Edison", "Benjamin Franklin", "Nikola Tesla", "Alessandro Volta"],
    ["H", "He", "Li", "Be"],
    ["Mount Everest", "K2", "Kangchenjunga", "Lhotse"],
  ]

  # Randomly select a question
  question = random.choice(questions)
  print(question)

  # Get the index of the selected question
  index = questions.index(question)

  # Get the correct answer for the selected question
  answer = answers[index]

  # Get the multiple choice options for the selected question
  choices = options[index]

  # Print the multiple choice options
  for i, option in enumerate(choices):
    print(str(i+1) + ". " + option)

  # Get the user's response to the question
  response = input("Your answer: ")

  # Check if the user's response is correct
  if response.lower() == answer.lower():
    print("Correct!")
  else:
    print("Incorrect. The correct answer is: " + answer)

quiz()

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...

my code simulates who will win an nfl game played 10 times.

import random

teams = ["eagles", "cowboys"]
def game():
    eagles = 0
    cowboys = 0
    for i in range (10):
        winner = random.choice(teams)
        if winner == "eagles":
            eagles +=1
        if winner == "cowboys":
            cowboys +=1
    print("Eagles won",eagles,"times", "Cowboys won",cowboys,"times")
game()
Eagles won 6 times Cowboys won 4 times