2

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:

  1. If the user's input matches the lottery in the exact order, the award is $10,000
  2. If all the digits in the user's input match all the digits in the lottery number, the award is $1,000
  3. 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!")

6 Answers6

4

As an alternative to using strings to do the comparisons, I'll suggest using arithmatic to get at the tens and ones digit of your numbers separately:

tens = num // 10 # the // operator does integer division
ones = num % 10  # the % operator finds the modulus

If you do this with both your usernum and lotterynum values, you can then find out how they match. Since this sounds like a homework assignment, I'll leave the details to you!

Blckknght
  • 100,903
  • 11
  • 120
  • 169
4

You may use set.

lottery_set = set('%02d' % lottery_num)
user_set = set('%02d' % user_num)
if lottery_num == user_num:
    print('10000')
elif lottery_set == user_set:
    print('3000')
elif lottery_set.intersection(user_set):
    print('1000')
else:
    print('0')
shantanoo
  • 3,617
  • 1
  • 24
  • 37
3

As far as an idea, I would say to convert your number to a string, and then use the chars in that string to determine the winning combo (e.g.: same chars same position, same chars different position, etc.)

Bart
  • 19,692
  • 7
  • 68
  • 77
MaxOvrdrv
  • 1,780
  • 17
  • 32
  • I don't know how to do that yet. We have not learned string conversion. If you could direct me to a place for a tutorial/instructions that would be great. Thank you for your help! – Vladislav Reshetnyak Mar 04 '13 at 20:23
  • 1
    http://stackoverflow.com/questions/961632/converting-integer-to-string-in-python (convert int to string) – MaxOvrdrv Mar 04 '13 at 23:40
  • 1
    http://mail.python.org/pipermail/tutor/2004-November/033569.html (extract a piece/char from the string in python) – MaxOvrdrv Mar 04 '13 at 23:43
  • So I've talked to the professor and he hinted at using // (see above), and when I mention strings he said it was extra work and not really necessary in this case. Also, we have not learned that in class yet so I was trying to stay on topic. Thank you for your help I still very greatly appreciate your effort and help! – Vladislav Reshetnyak Mar 06 '13 at 03:26
2

Well here is something that may help you out, you can do something like:

if(str(lottery_num)[0] == str(usernum)[0]):
    print "True"

The builtin str function will turn your integers into strings. In the above example, it will let you compare the first element of the string, with first element of the user string. Using this kind of accessing, you can solve your problem this way. The 0 represents the first element in this case, like a char array kind of.

eazar001
  • 1,572
  • 2
  • 16
  • 29
1

You need to isolate the digits so that they can be independently compared with each other.

An easy way to do this would be to convert the numbers into strings, then compare individual characters.

0

Here's the Complete assignment :)

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!")