SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.selections
Exercise:
Programming Excercises
Question:15 | ISBN:978013274719 | Edition: 6

Question

(Game: lottery) Revise Listing 4.10, Lottery.py, to generate a three-digit lottery number. The program prompts the user to enter a three-digit number and  determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is   $10,000.
2. If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is $1,000.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

SOURCE CODE

import random
# Generate a lottery number
lottery = random.randint(100, 999)
# Prompt the user to enter a guess
guess = eval(input("Enter your lottery pick(three digit): "))
# Get digits from lottery
lotteryDigit3 = lottery % 10
lottery //= 10
lotteryDigit2 = lottery % 10
lottery //= 10
lotteryDigit1 = lottery % 10
# Get digits from guess
guessDigit3 = guess % 10
guess //= 10
guessDigit2 = guess % 10
guess //= 10
guessDigit1 = guess % 10
print("The lottery number is", lottery)
# Check the guess
if guess == lottery:
    print("Exact match: you win $10,000")
elif (guessDigit3 == lotteryDigit3 and guessDigit2 == lotteryDigit1 and guessDigit1 == lotteryDigit2):
    print("Match all digits: you win $3,000")
elif (guessDigit1 == lotteryDigit1
        or guessDigit1 == lotteryDigit2
        or guessDigit1 == lotteryDigit3
        or guessDigit2 == lotteryDigit1
        or guessDigit2 == lotteryDigit2
        or guessDigit2 == lotteryDigit3
        or guessDigit3 == lotteryDigit1
        or guessDigit3 == lotteryDigit2
        or guessDigit3 == lotteryDigit3):
            print("Match one digit: you win $1,000")
else:
    print("Sorry,no match")

OUTPUT

Enter your lottery pick(three digit) : 324
The lottery number is 2
Match one digit: you win $1,000

 

0 0

Discussions

Post the discussion to improve the above solution.