From 9bead2e758151741638d22e730b8e55c61912472 Mon Sep 17 00:00:00 2001 From: cheeks <134818917+leftovertoast@users.noreply.github.com> Date: Mon, 3 Mar 2025 03:28:20 +0000 Subject: [PATCH] Completed 9-15 Lottery Analysis --- Chapter_09/9-15_lottery_analysis.py | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Chapter_09/9-15_lottery_analysis.py diff --git a/Chapter_09/9-15_lottery_analysis.py b/Chapter_09/9-15_lottery_analysis.py new file mode 100644 index 0000000..1bc6ce1 --- /dev/null +++ b/Chapter_09/9-15_lottery_analysis.py @@ -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.") +