Powerball Lottery

Tags: short, humour, simulation

The Powerball Lottery is an exciting way to lose small amounts of money. If you purchase a $2 ticket, you can pick six numbers: five drawn from 1 to 69, and a sixth “Powerball” number drawn from 1 to 26. The order of the numbers doesn’t matter. If the lottery selects your six numbers, you win $1.586 billion dollars! Except you won’t win, because your odds are 1 in 292,201,338. But if you spent $200 on 100 tickets, your odds would be … 1 in 2,922,013. You won’t win that either, but at least you’ll lose 100 times as much money. The more you like losing money, the more fun the lottery is!

To help you visualize how often you won’t win the lottery, this program simulates up to one million Powerball drawings and then compares them with the numbers you picked. Now you can have all the excitement of losing the lottery without spending money.

Fun fact: every set of six numbers is just as likely to win as any other. So the next time you want to buy a lottery ticket, pick the numbers 1, 2, 3, 4, 5, and 6. Those numbers are just as likely to come up as a more complex set.

powerball_lottery.py
  1"""Powerball Lottery, by Al Sweigart al@inventwithpython.com
  2A simulation of the lottery so you can experience the thrill of
  3losing the lottery without wasting your money.
  4This code is available at https://nostarch.com/big-book-small-python-programming
  5Tags: short, humor, simulation"""
  6
  7import random
  8
  9print('''Powerball Lottery, by Al Sweigart al@inventwithpython.com
 10
 11Each powerball lottery ticket costs $2. The jackpot for this game
 12is $1.586 billion! It doesn't matter what the jackpot is, though,
 13because the odds are 1 in 292,201,338, so you won't win.
 14
 15This simulation gives you the thrill of playing without wasting money.
 16''')
 17
 18# Let the player enter the first five numbers, 1 to 69:
 19while True:
 20    print('Enter 5 different numbers from 1 to 69, with spaces between')
 21    print('each number. (For example: 5 17 23 42 50)')
 22    response = input('> ')
 23
 24    # Check that the player entered 5 things:
 25    numbers = response.split()
 26    if len(numbers) != 5:
 27        print('Please enter 5 numbers, separated by spaces.')
 28        continue
 29
 30    # Convert the strings into integers:
 31    try:
 32        for i in range(5):
 33            numbers[i] = int(numbers[i])
 34    except ValueError:
 35        print('Please enter numbers, like 27, 35, or 62.')
 36        continue
 37
 38    # Check that the numbers are between 1 and 69:
 39    for i in range(5):
 40        if not (1 <= numbers[i] <= 69):
 41            print('The numbers must all be between 1 and 69.')
 42            continue
 43
 44    # Check that the numbers are unique:
 45    # (Create a set from number to remove duplicates.)
 46    if len(set(numbers)) != 5:
 47        print('You must enter 5 different numbers.')
 48        continue
 49
 50    break
 51
 52# Let the player select the powerball, 1 to 26:
 53while True:
 54    print('Enter the powerball number from 1 to 26.')
 55    response = input('> ')
 56
 57    # Convert the strings into integers:
 58    try:
 59        powerball = int(response)
 60    except ValueError:
 61        print('Please enter a number, like 3, 15, or 22.')
 62        continue
 63
 64    # Check that the number is between 1 and 26:
 65    if not (1 <= powerball <= 26):
 66        print('The powerball number must be between 1 and 26.')
 67        continue
 68
 69    break
 70
 71# Enter the number of times you want to play:
 72while True:
 73    print('How many times do you want to play? (Max: 1000000)')
 74    response = input('> ')
 75
 76    # Convert the strings into integers:
 77    try:
 78        numPlays = int(response)
 79    except ValueError:
 80        print('Please enter a number, like 3, 15, or 22000.')
 81        continue
 82
 83    # Check that the number is between 1 and 1000000:
 84    if not (1 <= numPlays <= 1000000):
 85        print('You can play between 1 and 1000000 times.')
 86        continue
 87
 88    break
 89
 90# Run the simulation:
 91price = '$' + str(2 * numPlays)
 92print('It costs', price, 'to play', numPlays, 'times, but don\'t')
 93print('worry. I\'m sure you\'ll win it all back.')
 94input('Press Enter to start...')
 95
 96possibleNumbers = list(range(1, 70))
 97for i in range(numPlays):
 98    # Come up with lottery numbers:
 99    random.shuffle(possibleNumbers)
100    winningNumbers = possibleNumbers[0:5]
101    winningPowerball = random.randint(1, 26)
102
103    # Display winning numbers:
104    print('The winning numbers are: ', end='')
105    allWinningNums = ''
106    for i in range(5):
107        allWinningNums += str(winningNumbers[i]) + ' '
108    allWinningNums += 'and ' + str(winningPowerball)
109    print(allWinningNums.ljust(21), end='')
110
111    # NOTE: Sets are not ordered, so it doesn't matter what order the
112    # integers in set(numbers) and set(winningNumbers) are.
113    if (set(numbers) == set(winningNumbers)
114        and powerball == winningPowerball):
115            print()
116            print('You have won the Powerball Lottery! Congratulations,')
117            print('you would be a billionaire if this was real!')
118            break
119    else:
120        print(' You lost.')  # The leading space is required here.
121
122print('You have wasted', price)
123print('Thanks for playing!')

https://inventwithpython.com/bigbookpython/project55.html