diff --git a/Chapter_09/9-14_lottery.py b/Chapter_09/9-14_lottery.py new file mode 100644 index 0000000..e244389 --- /dev/null +++ b/Chapter_09/9-14_lottery.py @@ -0,0 +1,48 @@ +# Exercise 9-14 Lottery +# 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.") +