Completed 9-15 Lottery Analysis
This commit is contained in:
parent
725060311c
commit
9bead2e758
48
Chapter_09/9-15_lottery_analysis.py
Normal file
48
Chapter_09/9-15_lottery_analysis.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# Exercise 9-15 Lottery Analysis
|
||||||
|
# Learning Objective: Continue experimentation with random module
|
||||||
|
|
||||||
|
from random import choice
|
||||||
|
import time
|
||||||
|
|
||||||
|
class LotteryTicket:
|
||||||
|
def __init__(self):
|
||||||
|
self.ticket = []
|
||||||
|
for i in range(1,11):
|
||||||
|
self.ticket.append(i)
|
||||||
|
self.letters = ['A', 'B', 'C', 'D', 'E']
|
||||||
|
self.letters.reverse()
|
||||||
|
for letter in range(1, 6):
|
||||||
|
to_add = self.letters.pop()
|
||||||
|
self.ticket.append(to_add)
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
winning_ticket = []
|
||||||
|
for i in range(1,5):
|
||||||
|
winning_ticket.append(choice(self.ticket))
|
||||||
|
return winning_ticket
|
||||||
|
winner = LotteryTicket()
|
||||||
|
|
||||||
|
lottery = []
|
||||||
|
|
||||||
|
my_ticket = [1, 3, 3, 7]
|
||||||
|
attempts = 0
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
while my_ticket != lottery:
|
||||||
|
while attempts <= 1000000:
|
||||||
|
lottery = winner.draw()
|
||||||
|
attempts += 1
|
||||||
|
print(lottery)
|
||||||
|
print(f"You've played {attempts} times and not won.")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
"Too many tries"
|
||||||
|
|
||||||
|
if my_ticket == lottery:
|
||||||
|
print("WINNER WINNER CHICKEN DINNER")
|
||||||
|
print(f"Winning ticket: {lottery}, your ticket: {my_ticket}\nCONGRATULATIONS!")
|
||||||
|
|
||||||
|
end = time.time()
|
||||||
|
elapsed_time = end - start
|
||||||
|
print(f"You won in a matter of {elapsed_time} seconds.")
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user