Project 1: a simple app about lottery win ticket statistics

Hello !! Hope you had nice week. Today, we begin 1rst part of Scripting Day!

part_1.jpg

As beginner, we must begin some simple projects before get into some little bit complex.

easy-work.png

Don't waste our time, let's start code.

scripting_day series in which are published some program scripts. Scripts can be:

  • simple scripts (contained in one or two file)
  • complex scripts (contained in more than two files; web app; etc.)

Today, we'll see a simple script which called: lottery

Winners walk!

lottery.jpeg

Let's suppose that in our city, a new lottery company (Winners walk) is established and before beginning their game, they want to check probabilities of where winners could come from. Since we like to help solve problems and provide solutions to needs, offer them a solution.

NB: I want to precise that this script is a simple example, and obviously not the actual process in a lottery game.

To code the script, i suppose that you've notion about:

  • variable
  • loop
  • function
  • display something with print()
  • condition

So, what do we know ?

Winners walk want to proceeds differently in comparison to competitors. They spotted towns in city, which are yopougon, cocody, Kumassi and plateau, for they 1rst product launch. At beginning, Winners walk sold 2517 tickets in Cocody for test.

After some researches, they found that:

  • yopougon population play 1.5 times more than cocody population.
  • plateau population play at game 3 times less than yopougon and 2.7 times less than population of kumassi.

Winning process

It's not enough to scratch ticket to win! So, there is a condition to win. When the ticket is scratched:

  • if the ticket reveals three suns, it's a winning ticket
  • if the ticket reveals something else, it's a losing ticket

Let's code!!!

code.jpg Upload on freeprik

We'll code in a file named lottery.py. Read again researches of Winners walk.

To solve issue, let's begin by calculate probability to win at cocody. We can note that cocody is the unknown variable, and also represent our starting point.

tickets_number = 2517 # sold tickets for test in cocody
winner_tickets = 0
cocody_win_chance = 0

def ticket_draw():
    """Choose a ticket, and return if is winner or not"""
    return randint(0, 1)

ticket_draw() function help us to say if ticket drawn is winning or losing ticket (we suppose that if value returned is 1, we've three suns if not, value returned will be 0) .

We know that we've 2517 tickets in cocody but we don't know the probability to win in cocody. So let's calculate it. We'll simulate a draw of all cocody available tickets in order to know pobability of win a prize. I'll use a loop, and i have to choose between a while loop and a for loop, but i prefer use a for loop.

# Suppose that Winners walk company decide to sell now 7530 tickets
new_tickets_sold = 7530
for x in range(0, tickets_number):
    result = ticket_draw()

    if result == 1:
        winner_tickets += 1

cocody_win_chance = winner_tickets/tickets_number

Now, we know cocody win chances. We can calculate others city win chances now. As remember, Yopougon population have more chance than cocody population to win a prize

# Yopougon population wins chance
yopougon_win_chance = 1.5*cocody_win_chance

# Plateau population wins chance
plateau_win_chance = yopougon_win_chance/3

# Kumassi population wins chance
kumassi_win_chance = 2.7*plateau_win_chance

All we've to do is to display cities wins probabilities

print(f'probability to win at cocody is: {cocody_win_chance}')
print(f'probability to win at yopougon is: {yopougon_win_chance}')
print(f'probability to win at plateau is: {plateau_win_chance}')
print(f'probability to win at kumassi is: {kumassi_win_chance}')

It's finished. We written our 1rst script! I don't know about you but me, i'm happy !!!

hourra.jpeg

Run the file to see results. go inside the file folder and run it.

$python3 lottery.py

Here is a screenshot of my results (with different sold tickets values).

Screenshot from 2019-06-16 14-15-17.png

Note that you can change sold tickets values to view results.

Here is complete lottery.py file

from random import randint

# 1) Calculate probability to win at cocody

tickets_number = 2517
winner_tickets = 0
cocody_win_chance = 0

def ticket_draw():
    """Choose a ticket, and return if is winner or not"""

    return randint(0, 1)

for x in range(0, tickets_number):
    result = ticket_draw()

    if result == 1:
        winner_tickets += 1

cocody_win_chance = winner_tickets/tickets_number

# 2) Calculate other towns win chance

''' Yopougon population have twice more chance
than cocody population to win a prize '''
yopougon_win_chance = 1.5*cocody_win_chance

# Plateau population wins three times less than yopougon
plateau_win_chance = yopougon_win_chance/3

# Kumassi population wins four times more than plateau
kumassi_win_chance = 2.7*plateau_win_chance


# 3) Display probabilities
print(f'probability to win at cocody is: {cocody_win_chance}')
print(f'probability to win at yopougon is: {yopougon_win_chance}')
print(f'probability to win at plateau is: {plateau_win_chance}')
print(f'probability to win at kumassi is: {kumassi_win_chance}')

If you didn't understood this part, please read it again!

Have a nice night and see you in two weeks to code a parking places handle application !!!

No Comments Yet