Unit 3 Lesson 16, Student Copy
- Hack #1 - Class Notes
- Hack #2 - Functions Classwork
- 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 used to simplify/modify certain variables
- Simulations can contain bias
- They can be used to simulate things as tests (ie, launching a bomb) when these actions are too dangerous/impractical to do in the real world
- Simulations can also be used to create situations that are too difficult to recreate in the real world (specific weather conditions for example)
- Random modules define series of objects that can be generated randomly
- Randomization helps simulations, as they can help predict many varying probablilities of a simulation
- Abstractions use conditionals to execute one part of the code only when a particular condition is met, repeat looping, simplification, and it does not request input from the user or display output to the user
- It is far cheaper to create a simulation than to do said actions in real life
erm hopefully thats enough notes i cant
import random
myclothes = ["red shoes", "green pants", "tie", "belt"]
def mycloset():
print(myclothes)
def addCloth():
a = input() # tells the code to issue an input function
print("Do you want to add any clothes? Type 'No' or the item name.")
if a != "No":
myclothes.append(a) #appends the input to the myclothes list
print("Object", a, "has been added.")
return myclothes
def removeCloth():
r = input() # tells the code to issue an input function
print("Do you want to remove any clothes? Type 'No' or the item name.")
if r != "No":
if (r in myclothes): # removes whatever the user types
myclothes.remove(r)
print(myclothes)
print("Object", r, "has been removed.")
mycloset()
addCloth()
print(myclothes)
removeCloth()
# add myclothes function
# trash myclothes function
import random
def randomnum(start, end, n=8): # function for generating random int
randNumList = []
for i in range(n): # in a range identified in the code
randNumList.append(random.randint(start,end)) #randomizes random numbers
print ("Randomly generated number list:", randNumList)
return randNumList
def converttobin(randNumList): # function for converting decimal to binary
divValue = randNumList # input (which is the randNumList)
remainder = 1
results = [] #empty results list for whatever is appended
for num in randNumList:
# convert eah num to binary string and append it to result list
divValue = num
x = '' # keeps track of 1's in the loop (so it can go infinitely)
while divValue > 0:
remainder = divValue % 2 # divides div val
divValue = (int)(divValue / 2) # integer x div value/2
x = str(remainder) + x
x = x.zfill(8)
results.append(x)
return results
# FIrst, a function for randomizing decimal number 0-255
# Next, a function for converting this to a 8-bit binary number (one bit per person)
# Lastly, a function to read the binary bits, and assign each person a label (zombie or naw) based on the position of 0s and 1s
def survivors(binary): # function to assign position
survivorstatus = ["Kohaku Oukawa", "Madara Mikejima", "Kaname Tojou", "Aira Shiratori" , "Kunikuzushi", "Kazuha", "Rook Hunt", "Rinne Amagi"]
# replace the names above with your choice of people in the house
i = 0 # index is 0 (empty)
zombie = 0 # a zombie is a 0
alive = 1 # an alive person is a 1
resultList = {} # empty list for the results
for b in binary:
person = survivorstatus[i] # sets a person equal to a survivor status
if b == '0': # if a person's status is 0 , they are a zombie, if not, they are alive. Yippee !!
resultList[person] = "Zombie"
else:
resultList[person] = "Alive"
i = i + 1 # each result gets appended to the list
print(resultList)
return resultList
for r in range(1):
ranNums = randomnum(1, 100, 8) # generates numbers from 1-100, and only 8 of them
binaryList = converttobin(ranNums) # the binary list is what has been given in the converttobin function
print(binaryList)
binary = random.choice(binaryList) # a random binary is chosen from the binary list
survivors(binary) # calls the survivors binary function to run
import random
def pull():
roll = [] # array for each result from rolling
num = int(input()) # how many times the dice is rolled
print("The amount of rolls you want is:", num) # printing number of rolls that are input
for n in range(num): # for the number that was put in the input, start of the for loop
result = random.randint(1, 6) # the results are any randomly generated number from 1 - 6
roll.append(result) # appends the randomly generated results to an array
return roll #returns function
dices = pull() # naming the function
print("Your dice rolls are:", dices) # final print of the array
- 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:
- The simulation is an abstraction and therefore cannot contain any bias
- The simulation may accidentally contain bias due to the exclusion of details.
- 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.
- answer options:
- 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
- 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.
- Yes, it's a simulation because it runs on a computer and includes both user input and computed output.
- Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.
- answer options
- 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
- 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
- Accurate accounting for the effects of wind conditions on the movement of the ball
- A baseball field that is textured to differentiate between the grass and the dirt
- answer options
- 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
- 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
- The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design.
- 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
- answer options
- Rinne Amagi wants to go to a casino, but Niki Shiina won't let him because he keeps wasting Niki's money, which is absolutely fair. He creates a computer simulation after much difficulty and suffering to gamble without Niki knowing, because he has an addiction. While his program is almost perfect, it lacks a good way to simulate a real casino, and the randomization is off. What are some things he could add to improve his simulation? (2 answers)
- Answer Options
- Niki Shiina dragging him out of the casino.
- A randomization function, where each result has different chances to appear based on a percentage.
- More items of different categories/ranks to make a bigger randomized simulation.
- Throw his computer against a wall and cry because he's weak.
- Answer Options
- Scaramouche has spent years trying to overthrow the Raiden Shogun, but to no avail. He is unfortunately the same element she is, and immunity is evil. He creates a simulation to determine what stance he should take to fight her, but thinks it's not close enough to reality. What could he add to his simulation to make it more realistic?
- Answer Options
- The Raiden Shogun's personal fox maiden.
- He should take into account whether the Shogun is injured or not.
- He should take the weather into account.
- He should consider his life choices and have an existencial breakdown.
- Answer Options
questions = 6
correct = 0
import random
questions_list = [
#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?"],
["Rinne Amagi wants to go to a casino, but Niki Shiina won't let him because he keeps wasting Niki's money, which is absolutely fair. He creates a computer simulation after much difficulty and suffering to gamble without Niki knowing, because he has an addiction. While his program is almost perfect, it lacks a good way to simulate a real casino, and the randomization is off. What are some things he could add to improve his simulation? (2 answers)"],
["Scaramouche has spent years trying to overthrow the Raiden Shogun, but to no avail. He is unfortunately the same element she is, and immunity is evil. He creates a simulation to determine what stance he should take to fight her, but thinks it's not close enough to reality. What could he add to his simulation to make it more realistic?"],
]
correct = [ # the answers in each list that are correct
["The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output."],
["Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences." ],
["Accurate accounting for the effects of wind conditions on the movement of the ball."],
["The simulation can be run more safely than an actual experiment.","The simulation can test the parachute design in a wide range of environmental conditions that may be difficult to reliably reproduce in an experiment."],
["A randomization function, where each result has different chances to appear based on a percentage.","More items of different categories/ranks to make a bigger randomized simulation."],
["He should take the weather into account."],
]
answerChoices = [ # all answer possibilities for each question
["The simulation is an abstraction and therefore cannot contain any bias.","The simulation may accidentally contain bias due to the exclusion of details.","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."],
["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.","Yes, it's a simulation because it runs on a computer and includes both user input and computed output.","Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences."],
["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.","Accurate accounting for the effects of wind conditions on the movement of the ball.","A baseball field that is textured to differentiate between the grass and the dirt."],
["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","The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design.","The simulation can test the parachute design in a wide range of environmental conditions that may be difficult to reliably reproduce in an experiment."],
["Niki Shiina dragging him out of the casino.","A randomization function, where each result has different chances to appear based on a percentage.","More items of different categories/ranks to make a bigger randomized simulation.","Throw his computer against a wall and cry because he's weak."],
["The Raiden Shogun's personal fox maiden.","He should take into account whether the Shogun is injured or not.","He should take the weather into account.","He should consider his life choices and have an existencial breakdown."],
]
points = [] #empty string to register points
s = "" # i probably don't need this list because it's in the loop anyways but i'm scared to break my code so it stays
def questionloop():
s = "" # list for inputs
count = 0 # starts the number of answered questions at 0
while (s != "quit" and count <= 6):
question = random.choice(questions_list) #randomizes questions
print(question)
print(answerChoices)
s = input() # allows user to input answer
print(s)
if s == "quit": # exits the quiz
print("Quiz exited.")
else:
answercheck(s, question)
count = count + 1 # adds to the question count so the loop stops when it hits 6
total = 0 #starts the total score at 0, adds
for score in points:
total += score
print("Your final score is:", total)
def answercheck(s, question):
i = questions_list.index(question) # refers to the index of the question list
matchedAnswers = list(filter(lambda item: item == s, correct[i])) # Filters wrong/right, if it's not in the list it's wrong, and that makes it appear as 'empty'
# (Tests, basically shows the len and if the len is >1, the answer is correct) print(matchedAnswers , str(len(matchedAnswers)))
if (len(matchedAnswers)) >= 1: # if the length was marked as 'none' (0) in the previous line, then it's measured (using len) and marked as incorrect
points.append(1) # appends 1 point to the list of points
print (points)
print("Correct.")
else:
points.append(0) # appends 0 points to the list of points
print(points)
print("Incorrect.")
# function to check if the answer was correct or not
questionloop()
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
# I kinda wanted to do this but I ran out of time
# I wanted to make a gacha simulator so just know that it will sit in this lesson like some sort of memorial.
# Rest in peace gacha simulator. In another world I gambled using you.
# 3 lists, 3 star 4star 5 star
# randint.(0, 100), generate 10 random ints
# if randint 0-75, print from the 3 star list, 76-99, print from the 4 star list, 100, print from the 5 star list