Python Puzzles
Imagine a bag that contains a single ball, which has an equal probability of being either black or white. Now, add a white ball to this bag. At this point, the bag holds two balls: one white and another that could be either white or black, each with an equal chance of 50%. If you randomly draw one ball from the bag, what would be the likelihood that the remaining ball inside the bag is white, given that you’ve drawn a white ball?
Don’t overcomplicate this conundrum; the probability that the remaining ball in the bag is white will be 50%.
Here is the Python code to run a simulation.
import random # number of simulations num_simulations = 1000000 # counter for the number of times the remaining ball is white given that a white ball was drawn count_white_remain = 0 for _ in range(num_simulations): # initial bag has a white ball and a ball that is equally likely to be black or white bag = ['white', random.choice(['white', 'black'])] # draw a ball drawn_ball = random.choice(bag) # if the drawn ball is white, remove it and check the color of the remaining ball if drawn_ball == 'white': bag.remove(drawn_ball) if bag[0] == 'white': count_white_remain += 1 # probability that the ball left in the bag is white given that a white ball was drawn prob_white_remain = count_white_remain / num_simulations print('Probability that the remaining ball is white given that a white ball was drawn:', prob_white_remain)