# 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 = random.randint(1,100)
def ran_ball(ball):
win = 0.45*random.randint(1,100)
lose = 0.51*random.randint(1,100)
if (win <= 1):
print("True if player wins")
elif (lose >= 1):
print("False if house wins")
ran_ball(1)
#I am unsure how to do this problem correctly as a function:<
False if house wins
#problem 1 but without a function
ball = random.randint(1,100)
if ball<50:
print("True if player wins")
else:
print("false if house wins")
print(f"the ball number is {ball}")
false if house wins
the ball number is 83
# Step 2.
# Create a function which simulates playing the game n_plays times; keep track of number of wins and losses
def game(n_plays):
wins = 0.
losses = 0.
for i in range(0, n_plays):
ball = random.randint(1,100)
play = False
if (ball>51):
play = True
wins = wins +1
print (f"Player wins, ball is {ball}")
else:
play = False
losses = losses +1
print(f"Player loses, ball is {ball}")
print(f"the total number of wins are {wins}")
print(f"the total number of losses are {losses}")
game(5)
Player wins, ball is 59
Player wins, ball is 65
Player loses, ball is 21
Player wins, ball is 68
Player wins, ball is 72
the total number of wins are 4.0
the total number of losses are 1.0
def game(n_plays):
wins = 0.
losses = 0.
money = 10000
for i in range(0, n_plays):
ball = random.randint(1,100)
play = False
if (ball>51):
play = True
wins = wins +1
money = money - 100
print (f"Player wins, ball is {ball}")
else:
play = False
losses = losses +1
money = money + 200
print(f"Player loses, ball is {ball}")
print(f"the total number of wins are {wins}")
print(f"the total number of losses are {losses}")
print(f"the resulting amount of money is {money}")
game(5)
Player wins, ball is 86
Player loses, ball is 12
Player loses, ball is 11
Player loses, ball is 17
Player loses, ball is 44
the total number of wins are 1.0
the total number of losses are 4.0
the resulting amount of money is 10700
# I dont understand what it means by 5 ganes 100 times
def game(n_games):
wins = 0.
losses = 0.
for i in range (0, n_games):
ball = random.randint(1,100)
funds = 0
if (ball>51):
wins = wins +1
prob_win = pr
print (f"Player wins, ball is {ball}")
else:
losses = losses +1
print(f"Player loses, ball is {ball}")
prob_win = .45*n_games
prob_loss = .51*n_games
prob = prob_loss+ prob_win
print(f"The discrete probability of winning is {prob/funds}")
#I know prob of winning/losing is .45*amount of money put in+.51*amount of money put in but I cannot think of a way to put it into the code
# 1. import random and set seed
# 2. Input target sales and number of sales persons for upcoming year; low and high
# percentages of sales for generating random numbers
# 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
# 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
#
# 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
#