Creating lottery game in Python

Hi, I’m trying to build a simple lotto lottery game. When a number picked by a user (from ticket_input) matches one of the randomly produced values (ticket_lottery), the player gets 1 point.
Now, I’m using only values from 0 to 9 as the code doesn’t work as expected in all steps.

I’m struggling with incrementing a value (points) as it engages the loop and conditional statements. The operation +=1 always refers to the value of points equals 0, so the total sum of points is wrong.

I’d be grateful for any help.

import random

ticket_lottery = random.sample(range(0, 9), k=7)

print("Provide 7 numbers from 0 to 9 (one by one- separated by spacebars):")
ticket_input = list(map(int, input().split()))

tl = sorted(ticket_lottery)
ti = sorted(ticket_input)

for item in ti:
    points = 0
    if ((item <= len(ti)) and (item in tl)):
            points += 1
            print("Item: {}, Lotto:{}, Points:{}".format(item, tl, points))
    elif((item <= len(ti)) and (item not in tl)):
            item += 1
            continue
    else:
            break

print("You got {} points in total".format(points))

Hi Cannelle,

It seems that you are comparing the items in ticket_lottery with items in ticket_input , correct? I would recommend using the zip() function to resolve this issue. So generate ticket_lottery and get ticket_input, then combine them with the zip() function like so:

result = list(zip(ticket_lottery, ticket_input))

This will make it easier for comparing two numbers from two separate lists.

import random

ticket_lottery = random.sample(range(0, 9), k=7)

print("Provide 7 numbers from 0 to 9 (one by one- separated by spacebars):")
ticket_input: list = list(map(int, input().split()))

# Combine ticket_lottery, and ticket_input with zip
result = list(zip(ticket_lottery, ticket_input))

# Display lottery ticket and User's choice 
print(f'Lottery Ticket: {" ".join([str(i) for i in ticket_lottery])}')
print(f'User\'s Choice : {" ".join([str(i) for i in ticket_input])}')

# Display results:
print(' \n ----------- Results ----------- ')
points = 0
# Iterate over both lists:
for lottery, user_choice in result:
    if lottery == user_choice:
        print(f'{lottery} & {user_choice} - match! {chr(10004)}')
        # If a match, gain a point
        points += 1
    else:
        print(f'{lottery} & {user_choice} - no match! {chr(10060)}')

# Display total points:
print('\nTotal points', points)

Screeen shot

image

Let me know if this resolve your issue, or if you have any further questions!

2 Likes

Thank you, @seraph776. I found your solution very valuable. The only thing I would change is that, in my case, the order of numbers does not matter. I coded this instead:

import random

ticket_lottery = random.sample(range(1, 20), k=7)

print("Provide 7 numbers from 1 to 20 (one by one- separated by spacebars):")
ticket_input = list(map(int, input().split()))

tl = sorted(ticket_lottery)
ti = sorted(ticket_input)

points = 0
for number in ti:
   if number in tl:
       points +=1

print("Lottery numbers are: {}".format(tl))
print("       You selected: {} and got {} points".format(ti, points))

if (points < 3):
   print("       Thank you for trying!".format(points))
if (points == 3):
   print("       Your prize is £20. Congrats!")
if (points == 4):
   print("       Your prize is £40. Congrats!")
if (points == 5):
   print("       Your prize is £100. Congrats!")
if (points == 6):
   print("       Your prize is £10000. Congrats!")
if (points == 7):
   print("       Your prize is £1000000. Congrats!")

Thank you once more for the reply. It’s nice seeing another solution to the task.

Best wishes!

1 Like

I like your lottery game better ~ it has more generous payout ! :sunglasses:
Great job!

1 Like

Just as an aside, have you considered how set logic can simplify your testing?

Consider,

>>> from random import sample
>>> a = set(sample(range(1, 10), k=3))
>>> a
{9, 2, 4}
>>> b = set(sample(range(1, 10), k=3))
>>> b
{8, 1, 3}
>>> a & b
set()
>>> a == a & b
False
>>> b = {2, 4, 9}
>>> a == a & b
True
>>> 
>>> len(a)
3
>>> b = {3, 4, 9}
>>> len(a & b)
2
>>> b = {2, 5, 6}
>>> len(a & b)
1

One would need 84 unique tickets for this lottery.

>>> from itertools import combinations as ncr
>>> [*ncr(range(1, 10),3)]
[(1, 2, 3), (1, 2, 4) .. (6, 8, 9), (7, 8, 9)]
>>> len([*ncr(range(1, 10),3)])
84
>>> 

Here is an JS example of evaluating an nCr problem; we had to create our own factorial program since JS doesn’t have one built in. It shows that there is good reason for why 6/49 didn’t go above that. The numbers are huge.

const f = function (n) {
    if (n == 0 || n == 1) {
        return 1
    }
    return n * f(n - 1)
}
undefined
f(7)    //    proof of concept
5040
f(9) / (f(6) * f(3))
84
f(49) / (f(43) * f(6))
13983816

Ping us if you wish to explore this.