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