{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Project: Mobile App for Lottery Addiction\n",
"\n",
"## Introduction : \n",
"\n",
"Many people start playing the lottery for fun, but for some this activity turns into a habit which eventually escalates into addiction. Like other compulsive gamblers, lottery addicts soon begin spending from their savings and loans, they start to accumulate debts, and eventually engage in desperate behaviors like theft.\n",
"\n",
"A medical institute that aims to prevent and treat gambling addictions wants to build a dedicated mobile app to help lottery addicts better estimate their chances of winning. The institute has a team of engineers that will build the app, but they need us to create the logical core of the app and calculate probabilities.\n",
"\n",
"For the first version of the app, they want us to focus on the 6/49 lottery and build functions that enable users to answer questions like:\n",
"\n",
"* What is the probability of winning the big prize with a single ticket?\n",
"* What is the probability of winning the big prize if we play 40 different tickets (or any other number)?\n",
"* What is the probability of having at least five (or four, or three, or two) winning numbers on a single ticket?\n",
"\n",
"The institute also wants us to consider historical data coming from the national 6/49 lottery game in Canada. The data set has data for 3,665 drawings, dating from 1982 to 2018.\n",
"\n",
"The scenario we're following throughout this project is fictional — the main purpose is to practice applying probability and combinatorics (permutations and combinations) concepts in a setting that simulates a real-world scenario."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Main Functions\n",
"\n",
"Throughout the project, we'll need to calculate repeatedly probabilities and combinations. As a consequence, we'll start by writing two functions that we'll use often:\n",
"\n",
"* A function that calculates factorials; and\n",
"* A function that calculates combinations."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def factorial(n):\n",
" res = n\n",
" # Base case: 0! = 1\n",
" if n == 0:\n",
" return 1\n",
" # Recursive case: n! = n * (n-1)!\n",
" else:\n",
" return res * factorial(n-1)\n",
"\n",
" \n",
"def combinations(n,k): \n",
" permutations = factorial(n)/factorial(n-k)\n",
" combinations = permutations / factorial(k)\n",
" return combinations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## One ticket probability\n",
"\n",
"We need to write a function, which takes a six unique numbers and prints the probability of winning the big prize.\n",
"\n",
"The engineer team told us that we need to be aware of the following details when we write the function:\n",
"\n",
"- Inside the app, the user inputs six different numbers from 1 to 49.\n",
"- Under the hood, the six numbers will come as a Python list and serve as an input to our function.\n",
"- The engineering team wants the function to print the probability value in a friendly way — in a way that people without any probability training are able to understand."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Check ticket validity\n",
"\n",
"We are going to write an interactive function to make sure that the user inputs six different numbers from 1 to 49.\n",
"\n",
"We will use the `'try/exception'` block in combination with the `'while loop'` to allow users to try multiple times until the input satisfies the condition and to make sure that this input does not lead to modes of failure such as when the user enters a non-integer number.\n",
"\n",
"The function will print messages with respect to what the user inputs and it will serve as an input to the function `'one_ticket_probability'`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def check_validity():\n",
" print(\"Please enter your 6 ticket numbers: \")\n",
" print(\"*********************************\") # output delimiter\n",
" numbers=[]\n",
" while len(numbers) < 6:\n",
" try:\n",
" number=input('Enter ticket number {}: '.format(len(numbers)+1))\n",
" print(\"*********************************\")\n",
" if int(number) in range(1,50) and int(number) not in numbers:\n",
" numbers.append(int(number))\n",
" else:\n",
" if int(number) not in range(1,50):\n",
" print(\"The number must be in the range from 1 to 49.\")\n",
" print(\"*********************************\")\n",
" else:\n",
" print(\"The number exists already.\")\n",
" print(\"*********************************\")\n",
" except :\n",
" print(\"The input is not valid.\")\n",
" print(\"*********************************\")\n",
" return numbers"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your 6 ticket numbers: \n",
"*********************************\n",
"Enter ticket number 1: 1\n",
"*********************************\n",
"Enter ticket number 2: 2\n",
"*********************************\n",
"Enter ticket number 3: 3\n",
"*********************************\n",
"Enter ticket number 4: 4\n",
"*********************************\n",
"Enter ticket number 5: 5\n",
"*********************************\n",
"Enter ticket number 6: 6\n",
"*********************************\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_validity()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below, we write the `one_ticket_probability` function, which takes the output of the function `check_validity` as an input and prints the probability of winning in percentage."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def one_ticket_probability():\n",
" ticket = check_validity()\n",
" possible_outcomes = combinations(49,6)\n",
" successful_outcomes = 1\n",
" chances = successful_outcomes * 100 / possible_outcomes\n",
" print(\"Your chances to win the big prize is {:.8f}%. In other words, you have a 1 in 13,983,816 chances to win.\".format(chances))\n",
" print(\"*********************************\")\n",
" return ticket,chances "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your 6 ticket numbers: \n",
"*********************************\n",
"Enter ticket number 1: 10\n",
"*********************************\n",
"Enter ticket number 2: 11\n",
"*********************************\n",
"Enter ticket number 3: 12\n",
"*********************************\n",
"Enter ticket number 4: 15\n",
"*********************************\n",
"Enter ticket number 5: 45\n",
"*********************************\n",
"Enter ticket number 6: 49\n",
"*********************************\n",
"Your chances to win the big prize is 0.00000715%. In other words, you have a 1 in 13,983,816 chances to win.\n",
"*********************************\n"
]
},
{
"data": {
"text/plain": [
"([10, 11, 12, 15, 45, 49], 7.151123842018516e-06)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"one_ticket_probability()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exploring the Canada lottery data set\n",
"The institute also wants us to consider the data coming from the national `6/49` lottery game in Canada. The data set contains historical data for 3,665 drawings, dating from 1982 to 2018 (the data set can be downloaded from [here](https://www.kaggle.com/datascienceai/lottery-dataset))."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The data set contains 3665 rows and 11 columns.\n"
]
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"data = pd.read_csv('649.csv')\n",
"print('The data set contains {} rows and {} columns.'.format(data.shape[0],data.shape[1]))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PRODUCT | \n",
" DRAW NUMBER | \n",
" SEQUENCE NUMBER | \n",
" DRAW DATE | \n",
" NUMBER DRAWN 1 | \n",
" NUMBER DRAWN 2 | \n",
" NUMBER DRAWN 3 | \n",
" NUMBER DRAWN 4 | \n",
" NUMBER DRAWN 5 | \n",
" NUMBER DRAWN 6 | \n",
" BONUS NUMBER | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 649 | \n",
" 1 | \n",
" 0 | \n",
" 6/12/1982 | \n",
" 3 | \n",
" 11 | \n",
" 12 | \n",
" 14 | \n",
" 41 | \n",
" 43 | \n",
" 13 | \n",
"
\n",
" \n",
" 1 | \n",
" 649 | \n",
" 2 | \n",
" 0 | \n",
" 6/19/1982 | \n",
" 8 | \n",
" 33 | \n",
" 36 | \n",
" 37 | \n",
" 39 | \n",
" 41 | \n",
" 9 | \n",
"
\n",
" \n",
" 2 | \n",
" 649 | \n",
" 3 | \n",
" 0 | \n",
" 6/26/1982 | \n",
" 1 | \n",
" 6 | \n",
" 23 | \n",
" 24 | \n",
" 27 | \n",
" 39 | \n",
" 34 | \n",
"
\n",
" \n",
" 3 | \n",
" 649 | \n",
" 4 | \n",
" 0 | \n",
" 7/3/1982 | \n",
" 3 | \n",
" 9 | \n",
" 10 | \n",
" 13 | \n",
" 20 | \n",
" 43 | \n",
" 34 | \n",
"
\n",
" \n",
" 4 | \n",
" 649 | \n",
" 5 | \n",
" 0 | \n",
" 7/10/1982 | \n",
" 5 | \n",
" 14 | \n",
" 21 | \n",
" 31 | \n",
" 34 | \n",
" 47 | \n",
" 45 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PRODUCT DRAW NUMBER SEQUENCE NUMBER DRAW DATE NUMBER DRAWN 1 \\\n",
"0 649 1 0 6/12/1982 3 \n",
"1 649 2 0 6/19/1982 8 \n",
"2 649 3 0 6/26/1982 1 \n",
"3 649 4 0 7/3/1982 3 \n",
"4 649 5 0 7/10/1982 5 \n",
"\n",
" NUMBER DRAWN 2 NUMBER DRAWN 3 NUMBER DRAWN 4 NUMBER DRAWN 5 \\\n",
"0 11 12 14 41 \n",
"1 33 36 37 39 \n",
"2 6 23 24 27 \n",
"3 9 10 13 20 \n",
"4 14 21 31 34 \n",
"\n",
" NUMBER DRAWN 6 BONUS NUMBER \n",
"0 43 13 \n",
"1 41 9 \n",
"2 39 34 \n",
"3 43 34 \n",
"4 47 45 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head(5)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PRODUCT | \n",
" DRAW NUMBER | \n",
" SEQUENCE NUMBER | \n",
" DRAW DATE | \n",
" NUMBER DRAWN 1 | \n",
" NUMBER DRAWN 2 | \n",
" NUMBER DRAWN 3 | \n",
" NUMBER DRAWN 4 | \n",
" NUMBER DRAWN 5 | \n",
" NUMBER DRAWN 6 | \n",
" BONUS NUMBER | \n",
"
\n",
" \n",
" \n",
" \n",
" 3660 | \n",
" 649 | \n",
" 3587 | \n",
" 0 | \n",
" 6/6/2018 | \n",
" 10 | \n",
" 15 | \n",
" 23 | \n",
" 38 | \n",
" 40 | \n",
" 41 | \n",
" 35 | \n",
"
\n",
" \n",
" 3661 | \n",
" 649 | \n",
" 3588 | \n",
" 0 | \n",
" 6/9/2018 | \n",
" 19 | \n",
" 25 | \n",
" 31 | \n",
" 36 | \n",
" 46 | \n",
" 47 | \n",
" 26 | \n",
"
\n",
" \n",
" 3662 | \n",
" 649 | \n",
" 3589 | \n",
" 0 | \n",
" 6/13/2018 | \n",
" 6 | \n",
" 22 | \n",
" 24 | \n",
" 31 | \n",
" 32 | \n",
" 34 | \n",
" 16 | \n",
"
\n",
" \n",
" 3663 | \n",
" 649 | \n",
" 3590 | \n",
" 0 | \n",
" 6/16/2018 | \n",
" 2 | \n",
" 15 | \n",
" 21 | \n",
" 31 | \n",
" 38 | \n",
" 49 | \n",
" 8 | \n",
"
\n",
" \n",
" 3664 | \n",
" 649 | \n",
" 3591 | \n",
" 0 | \n",
" 6/20/2018 | \n",
" 14 | \n",
" 24 | \n",
" 31 | \n",
" 35 | \n",
" 37 | \n",
" 48 | \n",
" 17 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PRODUCT DRAW NUMBER SEQUENCE NUMBER DRAW DATE NUMBER DRAWN 1 \\\n",
"3660 649 3587 0 6/6/2018 10 \n",
"3661 649 3588 0 6/9/2018 19 \n",
"3662 649 3589 0 6/13/2018 6 \n",
"3663 649 3590 0 6/16/2018 2 \n",
"3664 649 3591 0 6/20/2018 14 \n",
"\n",
" NUMBER DRAWN 2 NUMBER DRAWN 3 NUMBER DRAWN 4 NUMBER DRAWN 5 \\\n",
"3660 15 23 38 40 \n",
"3661 25 31 36 46 \n",
"3662 22 24 31 32 \n",
"3663 15 21 31 38 \n",
"3664 24 31 35 37 \n",
"\n",
" NUMBER DRAWN 6 BONUS NUMBER \n",
"3660 41 35 \n",
"3661 47 26 \n",
"3662 34 16 \n",
"3663 49 8 \n",
"3664 48 17 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.tail(5)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 3665 entries, 0 to 3664\n",
"Data columns (total 11 columns):\n",
"PRODUCT 3665 non-null int64\n",
"DRAW NUMBER 3665 non-null int64\n",
"SEQUENCE NUMBER 3665 non-null int64\n",
"DRAW DATE 3665 non-null object\n",
"NUMBER DRAWN 1 3665 non-null int64\n",
"NUMBER DRAWN 2 3665 non-null int64\n",
"NUMBER DRAWN 3 3665 non-null int64\n",
"NUMBER DRAWN 4 3665 non-null int64\n",
"NUMBER DRAWN 5 3665 non-null int64\n",
"NUMBER DRAWN 6 3665 non-null int64\n",
"BONUS NUMBER 3665 non-null int64\n",
"dtypes: int64(10), object(1)\n",
"memory usage: 315.0+ KB\n"
]
}
],
"source": [
"data.info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Function for Historical Data Check\n",
"\n",
"We're going to write a function that will enable users to compare their ticket against the historical lottery data in Canada and determine whether they would have ever won by now.\n",
"\n",
"The engineering team told us that we need to be aware of the following details:\n",
"\n",
"- Inside the app, the user inputs six different numbers from 1 to 49.\n",
"- Under the hood, the six numbers will come as a Python list and serve as an input to our function.\n",
"\n",
"The engineering team wants us to write a function that prints:\n",
"\n",
"- the number of times the combination selected occurred in the Canada data set; and\n",
"- the probability of winning the big prize in the next drawing with that combination."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We need first to extract all the winning six numbers from the historical data set, for that purpose we are going to write a function named `extract_numbers` that takes as input a row of the lottery dataframe and returns a set containing all the six winning numbers then we will use `extract_numbers` in combination with the `DataFrame.apply()` method to extract all the winning numbers."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def extract_numbers(row):\n",
" winning_six = set()\n",
" for i in range(4,10):\n",
" winning_six.add(row.iloc[i])\n",
" return winning_six "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0 {3, 41, 11, 12, 43, 14}\n",
"1 {33, 36, 37, 39, 8, 41}\n",
"2 {1, 6, 39, 23, 24, 27}\n",
"3 {3, 9, 10, 43, 13, 20}\n",
"4 {34, 5, 14, 47, 21, 31}\n",
"Name: winning_six, dtype: object"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['winning_six'] = data.apply(extract_numbers,axis=1)\n",
"data.winning_six.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below, we write the `check_historical_occurrence` function that takes in the output of the function `one_ticket_probability` (wich contains already the user numbers) and the historical numbers an inputs and prints information with respect to the number of occurrences and the probability of winning in the next drawing."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def check_historical_occurence(winning_six=data.winning_six):\n",
" ticket=one_ticket_probability()\n",
" numbers = set(ticket[0])\n",
" matches = data.winning_six == numbers\n",
" print(\"The combination {} has occurred {} time(s) previously.\".format(numbers,matches.sum()))\n",
" print(\"*********************************\")\n",
" if matches.sum()==0:\n",
" print(\"That combination has never occured. This doesn't mean it's more likely to occur now.\\nYour chances to win the big prize in the next drawing are still 0.0000072%.\")\n",
" else:\n",
" print('Your chances to win the big prize in the next drawing with that combination are still {:.8f} %.'.format(ticket[1]))\n",
" print(\"*********************************\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your 6 ticket numbers: \n",
"*********************************\n",
"Enter ticket number 1: 34\n",
"*********************************\n",
"Enter ticket number 2: 5\n",
"*********************************\n",
"Enter ticket number 3: 14\n",
"*********************************\n",
"Enter ticket number 4: 47\n",
"*********************************\n",
"Enter ticket number 5: 21\n",
"*********************************\n",
"Enter ticket number 6: 31\n",
"*********************************\n",
"Your chances to win the big prize is 0.00000715%. In other words, you have a 1 in 13,983,816 chances to win.\n",
"*********************************\n",
"The combination {34, 5, 14, 47, 21, 31} has occurred 1 time(s) previously.\n",
"*********************************\n",
"Your chances to win the big prize in the next drawing with that combination are still 0.00000715 %.\n",
"*********************************\n"
]
}
],
"source": [
"check_historical_occurence()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multi ticket probability\n",
"\n",
"Lottery addicts usually play more than one ticket on a single drawing, thinking that this might increase their chances of winning significantly. Our purpose is to help them better estimate their chances of winning so we're going to write a function that will allow the users to calculate the chances of winning for any number of different tickets.\n",
"\n",
"We've talked with the engineering team and they gave us the following information:\n",
"\n",
"- The user will input the number of different tickets they want to play (without inputting the specific combinations they intend to play).\n",
"- Our function will see an integer between 1 and 13,983,816 (the maximum number of different tickets).\n",
"- The function should print information about the probability of winning the big prize depending on the number of different tickets played.\n",
"\n",
"We are going to write a function named `multi_ticket_probability` that prints the probability of winning the big prize depending on the number of different tickets played."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def multi_ticket_probability():\n",
" possible_outcomes = combinations(49,6)\n",
" while True:\n",
" n=input('How many different tickets are you going to play: ')\n",
" print('*********************************')\n",
" try:\n",
" if int(n) in range(1,13983817):\n",
" chances = int(n)*100 / possible_outcomes\n",
" print(\"Your chances to win the big prize by playing {} ticket(s) are {:.10f} %.\".format(n,chances))\n",
" print('*********************************')\n",
" break\n",
" else:\n",
" print('Please enter a valid and reasonable number of tickets.')\n",
" print('*********************************')\n",
" except:\n",
" print('Invalid number of tickets')\n",
" print('*********************************')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"How many different tickets are you going to play: 910\n",
"*********************************\n",
"Your chances to win the big prize by playing 910 ticket(s) are 0.0065075227 %.\n",
"*********************************\n"
]
}
],
"source": [
"multi_ticket_probability()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Less winning numbers probability\n",
"\n",
"In most `6/49` lotteries there are smaller prizes if a player's ticket match two, three, four, or five of the six numbers drawn. As a consequence, the users might be interested in knowing the probability of having two, three, four, or five winning numbers.\n",
"\n",
"These are the engineering details we'll need to be aware of:\n",
"\n",
"- Inside the app, the user inputs:\n",
" * six different numbers from 1 to 49; and\n",
" * an integer between 2 and 5 that represents the number of winning numbers expected\n",
"- Our function prints information about the probability of having the inputted number of winning numbers.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def probability_less_6():\n",
" print(\"Calculate the probability of having two, three, four or five winning numbers.\")\n",
" print('*********************************')\n",
" while True: \n",
" n = input('Enter a number between 2 and 5: ')\n",
" print('*********************************')\n",
" try : \n",
" if int(n) in [2,3,4,5]:\n",
" combination_ticket = combinations(6,int(n)) \n",
" successful_outcomes = combination_ticket * combinations(49-6,6-int(n))\n",
" possible_outcomes = combinations(49,6)\n",
" proba_less_6 = successful_outcomes*100/possible_outcomes\n",
" print('Your chances to have exactly {} winning numbers are {} %'.format(n,'{:.10f}'.format(proba_less_6)))\n",
" print('*********************************')\n",
" break\n",
" else:\n",
" print('This number is out of range.')\n",
" print('*********************************')\n",
" except : \n",
" print('Invalid input')\n",
" print('*********************************')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculate the probability of having two, three, four or five winning numbers.\n",
"*********************************\n",
"Enter a number between 2 and 5: 910\n",
"*********************************\n",
"This number is out of range.\n",
"*********************************\n",
"Enter a number between 2 and 5: 5\n",
"*********************************\n",
"Your chance to have exactly 5 winning numbers is 0.0018449900 %\n",
"*********************************\n"
]
}
],
"source": [
"probability_less_6()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1,2,3,4 or 5 winning numbers\n",
"\n",
"We will create a function similar to `probability_less_6` which calculates the probability of having `at least` two, three, four or five winning numbers. For instance the the probability of having at least four winning numbers is the sum of these three probabilities:\n",
"\n",
"- The probability for having four winning numbers exactly\n",
"- The probability having five winning numbers exactly\n",
"- The probability for having six winning numbers exactly"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def probability_at_least():\n",
" print(\"Calculate the probability of at least two, three, four or five winning numbers.\")\n",
" print('*********************************')\n",
" while True: \n",
" n = input('Enter a number between 2 and 5: ')\n",
" print('*********************************')\n",
" try : \n",
" if int(n) in [2,3,4,5]:\n",
" possible_outcomes = combinations(49,6)\n",
" chances_at_least=[1/possible_outcomes]\n",
" for i in range(int(n),1,-1):\n",
" ticket_combinations = combinations(6,i)\n",
" successful_outcomes = ticket_combinations * combinations(43,6-i)\n",
" at_least = successful_outcomes *100 / possible_outcomes\n",
" chances_at_least.append(at_least)\n",
" print('Your chances to have at least {} winning numbers are {} %'.format(n,sum(chances_at_least)))\n",
" print('*********************************')\n",
" break\n",
" else:\n",
" print('This number is out of range.')\n",
" print('*********************************')\n",
" except : \n",
" print('Invalid number')\n",
" print('*********************************')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculate the probability of at least two, three, four or five winning numbers.\n",
"*********************************\n",
"Enter a number between 2 and 5: 44\n",
"*********************************\n",
"This number is out of range.\n",
"*********************************\n",
"Enter a number between 2 and 5: 0\n",
"*********************************\n",
"This number is out of range.\n",
"*********************************\n",
"Enter a number between 2 and 5: 5\n",
"*********************************\n",
"Your chance to have exactly 5 winning numbers is 15.101550320742207 %\n",
"*********************************\n"
]
}
],
"source": [
"probability_at_least()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Conclusion\n",
"\n",
"For the first version of the app, we coded four main functions using interactive inputs:\n",
"\n",
"* one_ticket_probability() — calculates the probability of winning the big prize with a single ticket\n",
"* check_historical_occurrence() — prints information on probability and historical occurrence in the Canada lottery data set at the same time\n",
"* multi_ticket_probability() — calculates the probability for any number of tickets between 1 and 13,983,816\n",
"* probability_less_6() — calculates the probability of having two, three, four or five winning numbers exactly\n",
"* probability_at_least()— calculates the probability of having at least two, three, four or five winning numbers"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}