In: Computer Science
Python program
A number game machine consists of three rotating disks labelled with the numbers 0 to 9 (inclusive). If certain combinations of numbers appear when the disks stop moving, the player wins the game.
The player wins if
otherwise the player loses.
Write a Python program to simulate this number game by randomly generating 3 integer values between 0 and 9 (inclusive), and printing whether the player has won or lost the game.
To randomly generate a number between and including 0 and 9, 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: 2 3 5 You lose.
Sample output 2:
Random numbers: 7 7 0 You win!
#importing random to access randint() method
import random
#randomly generating 3 numbers between 0 and 9
x = random.randint(0,9)
y = random.randint(0,9)
z = random.randint(0,9)
#printing generated numbers
print(x,y,z)
# Assuming that player lose
win = False;
#the first and third numbers are the same, and the second number
is less than 5
if x==z and y<5:
win = True
# all three numbers are the same
elif x==y and y==z:
win = True
# the sum of the first two numbers is greater than the third
number
elif x+y>z:
win = True
# if win is True, Player wins
if(win):
print("You win!")
# otherwise lose
else:
print("You lose.")
INDENTED CODE SCREENSHOT:
OUTPUT:
Run1:
Run2: