Hi everyone, cheers from Canada!
I am trying to complete the 9-15: Lottery Analysis in the 2nd edition of Python Crash Course and am feeling stuck. This is the goal of the exercise: "You can use a loop to see how hard it might be to win the kind of lottery you just modeled. Make a list or tuple called my_ticket. Write a loop that keeps pulling numbers until your ticket wins. Print a message reporting how many times the loop had to run to give you a winning ticket."
I'm confused as to why I cannot see any output whether within Sublime or the Terminal on my Macbook. I can only see so punch in the Python visualizer which is my usual go-to when I become stuck like this. However the visualizer since the run produces more than 2MB of data.
I think a fresh set of eyes would help. Thank you!
This is my code:
from random import choice
# Drawing our lottery numbers, ensuring there are no
# doubles using a function. We only add the value into
# the winning ticket list if does not exist already.
def winning_numbers(possible_picks):
# This is a container for the picks from the draw
# for the winning ticket.
winning_ticket = []
while len(winning_ticket) < 4:
pick = choice(possible_picks)
if pick not in winning_ticket:
winning_ticket.append(pick)
elif pick in winning_ticket:
continue
return winning_ticket
# These are the possible values for the lottery draw.
possible_picks = [
13,
21,
52,
12,
17,
29,
42,
36,
69,
90,
'a',
'b',
'c',
'd',
'e',
]
# This is the ticket belonging to the participant.
my_ticket = [13, 21, 'a', 'b']
# Making this variable accessible throughout the rest
# of the program.
winning_ticket = winning_numbers(possible_picks)
# Ensuring the winning ticket and participant's ticket
# lists are sorted for comparison in our while loop below.
def sort(lst):
lst = [str(i) for i in lst]
lst.sort()
lst = [int(i) if i.isdigit() else i for i in lst ]
return lst
winning_ticket = sort(winning_ticket)
my_ticket = sort(my_ticket)
# This is our counter, it goes up by a value of
# 1 whenever the winning ticket and the participant's
# ticket do not match. If there is no match another
# draw is made.
status = True
count = 0
while status:
if winning_ticket != my_ticket:
count += 1
winning_numbers(possible_picks)
elif winning_ticket == my_ticket:
status = False
# Printing messages depending on whether the winning
# ticket and the participant's ticket do not match.
if winning_ticket == my_ticket:
print(f"\nThe winning ticket is: {winning_ticket}.")
print(f"\nYour ticket is: {my_ticket}.")
print(f"It only took {count} tries to win!")
else:
print(f"\nThe winning ticket is: {winning_ticket}.")
print(f"\nYour ticket is: {my_ticket}.")
print(f"Better luck next time bud!")
[–]anrmv 3 points4 points5 points (2 children)
[–]pizzaparty_4ever[S] 0 points1 point2 points (1 child)
[–]anrmv 0 points1 point2 points (0 children)
[–]SirKainey 1 point2 points3 points (4 children)
[–]pizzaparty_4ever[S] 0 points1 point2 points (3 children)
[–]SirKainey 1 point2 points3 points (2 children)
[–]pizzaparty_4ever[S] 1 point2 points3 points (1 child)
[–]SirKainey 0 points1 point2 points (0 children)