I have an assignment in my python class that I'm struggling with.
Program
Here's the basic premise: Lottery Program. The program randomly generates a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rules:
- If the user's input matches the lottery in the exact order, the award is $10,000
- If all the digits in the user's input match all the digits in the lottery number, the award is $1,000
- If one digit in the user's input matches a digit in the lottery number, the award is $1,000
Basic format
Basically, I use randint to generate a two digit number (like say it generates 58) Then the user inputs a number of the same length (Although it's not specified, but for the sake of simplicity lets say the number is 10 to 99)
Then through a series of nested ifs the numbers are compared to 3 results and 1 exception.
Problem:
I don't have a clue as two how I compare the numbers in the way specified. I know all the basic operators, but in this case I don't see a way to use them (Except the perfectly matching numbers where you can use ==). I was thinking of an array (from my C/C++ class), but I'm not sure how to implement it here. Here's what I've done so far:
import random
import time
##Declare Variables
usernum=0.0
lottery_num=random.randint(10,99)
##Input
print("Welcome to the Lottery Program!")
usernum=int(input("Please enter a two digit number: "))
print("Calculating Results.")
for i in range(3):
time.sleep(1)
print(".")
##Calc & Output
if lottery_num==usernum:
print("All your numbers match in exact order! Your reward is $10,000!\n")
elif lottery_num== #WHAT DO HERE?
print("All your numbers match! Your reward is $3,000!\n")
elif lottery_num== #WHAT DO HERE?
print("One of your numbers match the lottery. Your reward is $1,000!\n")
else:
print("Your numbers don't match! Sorry!")
Solution
I've finally figured out how to do it with much help from you guys! Thank you very much! Here's the complete assignment for those interested as to what I did.
import random
import time
##Declare Variables
user_num=0
##lottery_num=random.randint(10,99)
lottery_num=12
##Input
print("Welcome to the Lottery Program!")
user_num=int(input("Please enter a two digit number: "))
print("Calculating Results.")
for i in range(3):
time.sleep(1)
print(".")
##Calc & Output
lottery_tens = lottery_num // 10
lottery_ones = lottery_num % 10
user_tens = user_num // 10
user_ones = user_num % 10
if lottery_num == user_num:
print("All your numbers match in exact order! Your reward is $10,000!\n")
elif lottery_tens == user_ones and lottery_ones == user_tens:
print("All your numbers match! Your reward is $3,000!\n")
elif lottery_tens == user_tens or lottery_ones == user_ones \
or lottery_ones == user_tens or lottery_tens == user_ones:
print("One of your numbers match the lottery. Your reward is $1,000!\n")
else:
print("Your numbers don't match! Sorry!")
##Same as Calc & Output using Sets.
##lottery_set = set('%02d' % lottery_num)
##user_set = set('%02d' % user_num)
##if lottery_num == user_num:
## print("All your numbers match in exact order! Your reward is $10,000!\n")
##elif lottery_set == user_set:
## print("All your numbers match! Your reward is $3,000!\n")
##elif lottery_set.intersection(user_set):
## print("One of your numbers match the lottery. Your reward is $1,000!\n")
##else:
## print("Your numbers don't match! Sorry!")