Question

In: Computer Science

A computer game simulates drawing three coloured balls from a basket. The basket contains red balls...

A computer game simulates drawing three coloured balls from a basket. The basket contains red balls (colour #1), green balls (colour #2) and yellow balls (colour #3). If certain combinations of red, green and/or yellow appear, the player wins the game.

The player wins if

  • the first and third balls are the same colour, and the second ball is a different colour from the first and third, or
  • all three balls are the same colour, or
  • the first and second balls are red (1) and the third ball is green (2);

otherwise the player loses.

Write a Python program to simulate this coloured ball game by randomly generating 3 integer values between 1 and 3 (inclusive), and printing whether the player has won or lost the game.

To randomly generate a number between and including 1 and 3, use the randintmethod from random library module. For example, x = random.randint(1,100) will automatically generate a random integer between 1 and 100 (inclusive) and store it in x.

Sample output 1:

Random numbers: 3 1 2
You lose.

Sample output 2:

Random numbers: 3 3 3
You win!

Solutions

Expert Solution

/*********************main.py*************************/

import random

ball1 = random.randint(1,3)
ball2 = random.randint(1,3)
ball3 = random.randint(1,3)
status = "";
if(ball1==ball2 and ball2 == ball3):
status = "Win"
elif(ball1==ball3 and ball2!=ball1):
status = "Win"
elif(ball1==1 and ball2==1 and ball3==2):
status = "Win"
else:
status = "Lose"
  
print("Random numbers: ",ball1,ball2,ball3)
print("You",status,"!")

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

A basket contains 100 balls.40 are red,45 are orange and 15 are yellow.Three balls will be...
A basket contains 100 balls.40 are red,45 are orange and 15 are yellow.Three balls will be drawn out one at a time at random with replacement.Match the probabilities. (a) P(all three draws are red) (b) P(all three draws are orange) (c) P(at least one draw is red) (d) P(at least one draw is orange) 2.Refer to the previous question about the balls in the basket.Instead of drawing out three balls one at a time with repalcement, suppose we selected balls...
A bucket contains three red balls and four white balls. Two balls are drawn from the...
A bucket contains three red balls and four white balls. Two balls are drawn from the bucket in succession without replacement. What is the probability of getting two white balls, ie. a white ball on the first draw followed by a white ball on the second draw?
A bag contains 8 red balls and some white balls. If the probability of drawing a white ball is half of the probability of drawing a red ball then find the number of white balls in the bag.
A bag contains 8 red balls and some white balls. If the probability of drawing a white ball is half of the probability of drawing a red ball then find the number of white balls in the bag.
An urn contains 6 red balls and 4 green balls. Three balls are chosen randomly from...
An urn contains 6 red balls and 4 green balls. Three balls are chosen randomly from the urn, without replacement. (a) What is the probability that all three balls are red? (Round your answer to four decimal places.) (b) Suppose that you win $20 for each red ball drawn and you lose $10 for each green ball drawn. Compute the expected value of your winnings.
A box contains three white balls, two black balls, and one red ball. Three balls are...
A box contains three white balls, two black balls, and one red ball. Three balls are drawn at random without replacement. Let Y1 be the number of white balls drawn, and Y2 the number of black balls drawn. Find the distribution of Z = Y1 × Y2
A bag contains 4 red balls and 8 green balls. (a). (5’) Three balls are selected...
A bag contains 4 red balls and 8 green balls. (a). (5’) Three balls are selected from the bag randomly. What is the probability that 1 red ball and 2 green balls are selected? (b). (10’) Three balls are selected from the bag sequentially without replacement, and the first ball is discarded without observing the color. If the third ball is green, what is the probability that the second ball is red? (c). (10’) Two balls are selected from the...
An urn contains 6 red balls, 7 white balls, and 8 blue balls. a) If three...
An urn contains 6 red balls, 7 white balls, and 8 blue balls. a) If three balls are sampled without replacement, find probability that all are different colors b) If three balls are sampled with replacement, find the probability that are different colors. c) i n balls sampled with replacement, find probability that all are red. d) If nballs sampled with replacement, find the probability that all are the same color.
Bag 1 contains 3 red balls and 7 green balls. Bag 2 contains 8 red balls...
Bag 1 contains 3 red balls and 7 green balls. Bag 2 contains 8 red balls and 4 green balls. Bag 3 contains 5 red balls and 11 green balls. Bag 1 is chosen twice as often as either Bag 2 or Bag 3 (which have the same probability of being chosen). After a bag is chosen, a ball is chosen at random from that bag. Calculate the probability that: a) a red ball is chosen b) a red ball...
Box A contains 6 red balls and 3 green balls, whereas box B contains 3 red...
Box A contains 6 red balls and 3 green balls, whereas box B contains 3 red ball and 15 green balls. Stage one. One box is selected at random in such a way that box A is selected with probability 1/5 and box B is selected with probability 4/5. Stage two. Finally, suppose that two balls are selected at random with replacement from the box selected at stage one. g) What is the probability that both balls are red? h)...
An urn contains red and green colored balls. You draw a sample of five balls from...
An urn contains red and green colored balls. You draw a sample of five balls from the urn. (a) Suppose the urn contains 18 balls, half of which are red. What is the probability that the sample contains a red ball? (b) Suppose the urn contains 144 balls, half of which are red. What is the probability that the sample contains a red ball? (c) Suppose the urn contains 144 balls, 10 of which are red. What is the probability...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT