QA Graphic

Simple Lottery Program

Learn By Example

One of the books that got me into programming was "Perl by Example" by Ellie Quigley. I learned a lot about how to perform certain tasks using simple examples.

This week's post is highlighting an example of how I learned using the book. The following is an example of creating a "Lottery Picker" Python application. This is a pretty basic example of using various Python functionality to generate a random set of numbers.

Power Python

PowerBall Quick Pick

Players must select 5 numbers from 1 to 69. Then they must pick one additional number from a range of 1 to 26.

In this example, the program outputs 4 games. I use the sort list function to display the smaller numbers first.

import random
# Powerball Quick Pick
GameBalls = list(range(1,69))
PowerBall = list(range(1,26))
random.shuffle(GameBalls)
random.shuffle(PowerBall)
for x in range(4):
	mywin = GameBalls[0:5]
	mywin.sort()
	print(f"Winning Numbers: {mywin} with a Powerball number {PowerBall[0]}")
	random.shuffle(GameBalls)
	random.shuffle(PowerBall)

Sample Output

Winning Numbers: [2, 11, 15, 27, 58] with a Powerball number 10
Winning Numbers: [9, 54, 58, 63, 67] with a Powerball number 3
Winning Numbers: [10, 16, 18, 47, 62] with a Powerball number 4
Winning Numbers: [3, 35, 38, 41, 56] with a Powerball number 3

Odds of Winning Powerball

The odds of winning the PowerBall is 11,688,053 to 1. You have a one in 91 chance of matching at least 1 number.

Things This Code Demonstrates

The above piece of code answers some common "how do I" questions:

  • How do I create a python list not starting from 0?
  • How doI shuffle a list in Python?
  • In Python, How do I create a new list from another list?
  • How do I sort a randomize python list?
  • After shuffling a list how do I sort it?

Future Site Content

I'll add a PowerBall number picker to the Random selection.

  Filed under the Python category. - Permalink

About

Cool Tricks and Tips using Python 3

Elsewhere

  1. Twitter
  2. Facebook