-3

I am writing a lottery program, for my class. I have looked on stackoverflow for an answer but all I have found have been to advanced. I was wondering if there is a way to shorten the if statements and have fewer of them.

import random

def lottery():                      
    lottoNumber1 = random.randint(1,50)
    print(lottoNumber1)
    lottoNumber2 = random.randint(1,50)
    print(lottoNumber2)
    lottoNumber3 = random.randint(1,50)
    print(lottoNumber3)
    return lottoNumber1,lottoNumber2,lottoNumber3

userChoice1 = int(input('Choose a number between 1 and 50: '))
userChoice2 = int(input('Choose a number between 1 and 50: '))
userChoice3 = int(input('Choose a number between 1 and 50: '))

lottoNumber1, lottoNumber2, lottoNumber3 = lottery()

if userChoice1 == lottoNumber1:
if userChoice1 == lottoNumber2:
    if userChoice1 == lottoNumber3:
        if userChoice2 == lottoNumber1:
            if userChoice2 == lottoNumber2:
                if userChoice2 == lottoNumber3:
                    if userChoice3 == lottoNumber1:
                        if userChoice3 == lottoNumber2:
                            if userChoice3 == lottoNumber3:

        print('You win $1,000')
else:
    print('Try it again!')

main()
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Reid
  • 1
  • 3
  • Are duplicates allowed among `lottoNumber`s ? – CristiFati Aug 28 '15 at 15:50
  • 1
    The code you way you have it right now won't work at all. Every user input would have to match every lotto number. So they would all have to be the same number. Also, you're calling `main()` but there is no main function. – Morgan Thrapp Aug 28 '15 at 15:51
  • Yeah, you only need to do `if userChoice1 == lottoNumber1 and userChoice2 == lottoNumber2 and userChoice3 == lottoNumber3:` – Shotgun Ninja Aug 28 '15 at 15:54
  • According to this poor idea of a code I would expect that the order of the values doesn't matter. – Matthias Aug 28 '15 at 15:56
  • related: http://stackoverflow.com/questions/9623114/check-if-two-unordered-lists-are-equal – NightShadeQueen Aug 28 '15 at 16:19

2 Answers2

1

If you create two sets, one holding the lottery numbers and the other holding the user choices then one if statement could compare them:

import random

# Use sets to ensure lottery numbers and user choices are unique
lottoNumbers = set()
userChoices = set()

while len(lottoNumbers) < 3:
    lottoNumbers.add(random.randint(1,50))

while len(userChoices) < 3:
    userChoices.add(input('Choose a number between 1 and 50: '))

print(lottoNumbers)
print(userChoices)

if lottoNumbers == userChoices:
    print('You win $1,000')
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0

This will work for you.

from random import randint

def lottery():                      
    return [randint(1, 50) for x in range(3)]

user_choices = list(map(int, [input('Choose a number between 1 and 50: ') for x in range(3)]))
lotto_numbers = lottery()
print(user_choices)
print(lotto_numbers)
if any(user_choice == lotto_number for user_choice, lotto_number in zip(sorted(user_choices), sorted(lotto_numbers)))
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
  • Maybe you should sort the values before you compare them. With your current code `[3, 2, 1]` would be different from `[1, 2, 3]`. – Matthias Aug 28 '15 at 15:54