# import random library and set seed
import random
random.seed(123456)
# Step 1.
# Define a function which randomly draws a ball with an integer between 1 and 100 on it;
# then returns True if Player
# wins and returns False if House wins.
# Add a print statement to function to print random integer and test out function
# Add comment statements to function
#
ball_n = random.randint (1,100)
def random_win () :
ball_n = random.randint (1,100)
if (ball_n > 51) : #when the player draws a ball with a number greater than 51, they win (true)
result = "True"
print (result)
else: #when player draws ball with number less than 51, the house wins (false)
result = "False"
print (result)
print (ball_n)
return (result , ball_n)
random_win ()
False
4
# Step 2.
# Create a function which simulates playing the game n_plays times; keep track of number of wins and losses
#
n_plays = 1
for i in range (0,n_plays):
n_win = 0
n_loss = 0
if (result == 'True') :
n_win = n_win + 1
else :
n_loss = n_loss + 1
print (f'win =', n_win)
print (f'loss =' , n_loss)
Execution error
NameError: name 'result' is not defined
n_plays = 3
wins = [ 0 for i in range (0,12)]
print (wins)
losses = [ 0 for i in range (0,12)]
print (losses)
for i in range (0,n_plays) :
win_or_loss = random_win ()
print (win_or_loss)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
True
57
('True', 57)
False
44
('False', 44)
True
56
('True', 56)
# 1. import random and set seed
import random
random.seed(123456)
import numpy as np
# 2. Input target sales and number of sales persons for upcoming year; low and high
# percentages of sales for generating random numbers
n_sales_reps = [1, 2, 3, 4, 5]
target_sales = [150000 , 200000, 95000, 350000, 425000]
low_range = [round(150000*.85, 3), round(200000*.85, 3), round(95000*.85, 3), round(350000*.85, 3), round(425000*.85, 3)]
high_range = [round(150000*1.1, 3) , round(200000*1.1, 3), round(95000*1.1, 3), round(350000*1.1, 3), round(425000*1.1, 3)]
print (n_sales_reps)
print (low_range)
print (high_range)
[1, 2, 3, 4, 5]
[127500.0, 170000.0, 80750.0, 297500.0, 361250.0]
[165000.0, 220000.0, 104500.0, 385000.0, 467500.0]
# 3. Generate a random sales amount for each rep and check that it is in the correct range
# Use a for loop; round answer to nearest cent
n = 0
for i in range (0,len (n_sales_reps)) :
random_sale = random.randint (low_range [n], high_range [n])
n = n + 1
print (round (random_sale, 3))
146483
171928
86473
297792
448266
# 4. Add to your loop in # 3 to determine the commission for each rep
# (you need an if-elif-else construct to determine rate); keep a running tab of
# the sum of all commissions
# round all dollar amounts to nearest cent
#
n = 0
total_commissions = 0
for i in range (0,len (n_sales_reps)) :
random_sale = random.randint (low_range [n], high_range [n])
if (random_sale < (target_sales [n] * 0.9)):
comission = (random_sale*0.02)
elif (random_sale < (target_sales [n] * 1)):
commission = (random_sale*0.03)
else :
commission = (random_sale * 0.04)
print (f'Representative number {n_sales_reps [n]} sold the house for {random_sale}, so they got {round(commission,2)}.')
total_commissions = total_commissions + commission
print (f'Total commissions = {round (total_commissions,2)}')
n = n + 1
Representative number 1 sold the house for 147071, so they got 4412.13.
Total commissions = 4412.13
Representative number 2 sold the house for 199006, so they got 5970.18.
Total commissions = 10382.31
Representative number 3 sold the house for 97536, so they got 3901.44.
Total commissions = 14283.75
Representative number 4 sold the house for 378920, so they got 15156.8.
Total commissions = 29440.55
Representative number 5 sold the house for 404078, so they got 12122.34.
Total commissions = 41562.89
# 5. Add a loop around your code in # 4 to do 10,000 simulations
# Only print out the average commission for all 10,000 simulations
# Don't print out all the information from Step # 4 - it will be way too much so remove
# or comment out those print statements
#
n = 0
total_commissions = 0
n_sim = 10000
for q in range (n_sim):
for i in range (0,len (n_sales_reps)) :
random_sale = random.randint (float(low_range [n]), float(high_range [n]))
if (random_sale < (target_sales [n] * 0.9)):
comission = (random_sale*0.02)
elif (random_sale < (target_sales [n] * 1)):
commission = (random_sale*0.03)
else :
commission = (random_sale * 0.04)
#print (f'Representative number {n_sales_reps [n]} sold the house for {random_sale}, so they got {round(commission,2)}.')
total_commissions = total_commissions + commission
#print (f'Total commissions = {round (total_commissions,2)}')
n = n + 1
avg_comission = round (total_commissions/float(n_sim),2)
print (avg_comission)
26459.49