Unit 3 Section 16 Homework and Challenge
- Hack #1 - Class Notes
- Hack #2 - Functions Classwork
- Example: Coin Flip
- Hack #3 - Binary Simulation Problem
- Hack #4 - Thinking through a problem
- Hack 5 - Applying your knowledge to situation based problems
- Hack #6 / Challenge - Taking real life problems and implementing them into code
Hack #1 - Class Notes
- Simulations are abstractions that mimic more complex objects or phenomena from the real world
- Purposes:
- Drawing inferences without the contraints of the real world
- Simulations use varying sets of values to reflect the changing state of a real phenomenon
- Often, when developing a simulation, it is necessary to remove specific details or simplify aspects
- Simulations can often contain bias based on which details or real-world elements were included/excluded
- Simulations allow the formulation of hypotheses under consideration
- Variability and randomness of the world is considered using random number generators
- Examples:
- Rolling dice, spinners, molecular models, analyze chemicals/reactions...
- A simulation allows us to explore this question without real world contraints of money, time, safety
- Since the simulation won't be able to take all variables into control, it may have a bias towards one answer
import random # importing the package - random
x = random.randint(1,100) #getting a random integer between 1 and 100
print(x) # printing the random integer
My-closet function:
import random
def mycloset(): #def function
myclothes = ["red shoes", "green pants", "tie", "belt"] # defining clothes list
newclothes = ["black shoes", "white shirt", "blue socks", "red jacket"] # defining new clothes list
x = random.randint(1, len(newclothes)) # Creating random integer between 1 and the length of new clothes list
y = random.randint(1, len(myclothes)) #Creating random integer between 1 and the length of clothes list
user = input("Please decide whether to add or trash an item") #creating user input
if user.lower() == "add": # if user chooses add
myclothes.append(newclothes[x]) # adding a random item from new clothes into myclothes
elif user.lower() == "trash": #if user chooses trash
myclothes.remove(myclothes[y]) #removing a random item from myclothes list
return myclothes # returning the list
mycloset() # calling function
import random
def coinflip(): #def function
randomflip = random.randint(0, 2) #picks 0, 1, or 2 randomly - creating a weighted coin with 33% fail and 66% head
if randomflip == 0: #assigning 0 to be tails--> if 0 is chosen then it will print, "tails"
print("Tails")
else:
if randomflip == 1 or randomflip == 2: #assigning 1 or 2 to be heads--> if 1 or 2 is chosen then it will print, "heads"
print("Heads")
#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()
import random
def randomnum(): # function for generating random int
x = random.randint(0, 255) # random integer from 0 to 255
return x # returning random integer
def converttobin(n): # function for converting decimal to binary
binary = bin(n).replace("0b", "0")
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"]
num = 0
for bin in binary:
if bin == "0":
print(survivorstatus[num] + " is a zombie")
else:
print(survivorstatus[num] + " is a survivor")
num += 1
survivors(converttobin(randomnum()))
# replace the names above with your choice of people in the house
import random
def diceroll():
x = random.randint(1,6)
return x
roll1 = diceroll()
roll2 = diceroll()
roll3 = diceroll()
roll4 = diceroll()
roll5 = diceroll()
roll6 = diceroll()
print("Roll 1:", roll1)
print("Roll 2:", roll2)
print("Roll 3:", roll3)
print("Roll 4:", roll4)
print("Roll 5:", roll5)
print("Roll 6:", roll6)
import random
def Quiz(question, choices):
global word
print("Question: " + question)
for c in choices:
print(c)
word = input()
return word
questions = 6
correct = 0
list = [
{"question":"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.?", "correct_answer":"the simulation may accidentally contain bias due to the exclusion of details", "incorrect_answer":["if the simulation is found to contain bias, then it is not possible to remove the bias from the simulation", "the only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output"]},
{"question":"Kack 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?", "correct_answer":"yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences", "incorrect_answer":["no, it's not a simulation because it does not include a visualization of the results", "no, it's not a simulation because it does not include all the details of his life history and the future financial environment"]},
{"question":"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?", "correct_answer":"accurate accounting for the effects of wind conditions on the movement of the ball", "incorrect_answer":["realistic sound effects based on the material of the baseball bat and the velocity of the hit", "a depiction of an audience in the stands with lifelike behavior in response to hit accuracy"]},
{"question":"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?", "correct_answer":"the simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design", "incorrect_answer":["the simulation will not contain any bias that favors one body type over another, while an experiment will be biased", "the simulation can be run more safely than an actual experiment"]},
{"question":"What is a simulation?", "correct_answer":"mimic real world situations", "incorrect_answer":["a procedure in coding", "a function of loops"]},
{"question":"What is always true of a simulation of a natural phenomenon?", "correct_answer":"it abstracts away some details of the real world", "incorrect_answer":["the simulation will always be accurate", "the simulation will always be focused on how damagineg natural phenomenons can be"]}]
for i in range(len(list)):
multiple_choices = []
multiple_choices.append(list[i]["correct_answer"])
multiple_choices.append(list[i]["incorrect_answer"][0])
multiple_choices.append(list[i]["incorrect_answer"][1])
Quiz(list[i]["question"], multiple_choices)
if word.lower() == list[i]["correct_answer"]:
print("Answer is correct")
correct += 1
else:
print("Answer is incorrect")
print( "You scored " + str(correct) +"/" + str(questions))
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...
runner1speed = 5
runner2speed = 7
runner1pos = 0
runner2pos = 0
totaldistance = 600
uphillspeed = 0
while runner1pos < totaldistance and runner2pos < totaldistance:
runner1pos += runner1speed
runner2pos += runner2speed
for x in range(340, 400):
uphillspeed = x//70
runner1speed = uphillspeed
runner2speed = uphillspeed
print("Runner 1 position:", runner1pos)
print("Runner 2 position:", runner2pos)