Suppose you want to develop a program to play lottery. The program randomly generates a Lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rule: 1. If the user matches the lottery in exact order , the awards is $10,000. 2. If the user input matches the lottery, the awards is $3,000. 3. If one digit in the user input matches a digit in the lottery, the awards is $1,000.

Respuesta :

Python Code with Explanation:

import random  

# get a two digit number from the user

guess=int(input("Please enter a two digit number: "))

# here we are generating a two digit random number from 10 to 99

lottery=random.randrange(10, 99)

# print the Lottery digits

print("Lottery digits are: ",lottery)

# we separated the two digits, dividing by 10 returns the 1st digit and modulus of 10 returns the 2nd digit

lottery_1st = lottery // 10

lottery_2nd = lottery % 10

guess_1st = guess // 10

guess_2nd = guess % 10

# now we compare all the cases given in the question using if elif commands

if lottery == guess:

   print("All digits match in exact order! You won $10,000")

elif lottery_1st == guess_2nd and lottery_2nd == guess_1st:

   print("All digits match but not in exact order! You won $3,000!")

elif lottery_1st == guess_1st or lottery_2nd == guess_2nd or lottery_2nd == guess_1st or lottery_1st == guess_2nd:

   print("Only one digit matches! You won $1,000!")

else:

   print("None of the digits match! Better Luck next time!")

Output:

Test 1:

Please enter a two digit number: 84

Lottery digits are: 48

All digits match but not in exact order! You won $3,000

Test 2:

Please enter a two digit number: 71

Lottery digits are: 28

None of the digits match! Better Luck next time!

Test 3:

Please enter a two digit number: 45

Lottery digits are: 45

All digits match in exact order! You won $10,000

Test 4:

Please enter a two digit number: 40

Lottery digits are: 14

Only one digit matches! You won $1,000

Ver imagen nafeesahmed
Ver imagen nafeesahmed
Ver imagen nafeesahmed
Ver imagen nafeesahmed