In: Computer Science
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
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!
/*********************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 :)