Sorry I wasn’t direct with my issue.
I will post Eric’s solution in full first and then quote that which is unclear to me, here is his solution:
from random import choice
def get_winning_ticket(possibilities):
"""Return a winning ticket from a set of possibilities."""
winning_ticket = []
# We don't want to repeat winning numbers or letters, so we'll use a
# while loop.
while len(winning_ticket) < 4:
pulled_item = choice(possibilities)
# Only add the pulled item to the winning ticket if it hasn't
# already been pulled.
if pulled_item not in winning_ticket:
winning_ticket.append(pulled_item)
return winning_ticket
def check_ticket(played_ticket, winning_ticket):
# Check all elements in the played ticket. If any are not in the
# winning ticket, return False.
for element in played_ticket:
if element not in winning_ticket:
return False
# We must have a winning ticket!
return True
def make_random_ticket(possibilities):
"""Return a random ticket from a set of possibilities."""
ticket = []
# We don't want to repeat numbers or letters, so we'll use a while loop.
while len(ticket) < 4:
pulled_item = choice(possibilities)
# Only add the pulled item to the ticket if it hasn't already
# been pulled.
if pulled_item not in ticket:
ticket.append(pulled_item)
return ticket
possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
winning_ticket = get_winning_ticket(possibilities)
plays = 0
won = False
# Let's set a max number of tries, in case this takes forever!
max_tries = 1_000_000
while not won:
new_ticket = make_random_ticket(possibilities)
won = check_ticket(new_ticket, winning_ticket)
plays += 1
if plays >= max_tries:
break
if won:
print("We have a winning ticket!")
print(f"Your ticket: {new_ticket}")
print(f"Winning ticket: {winning_ticket}")
print(f"It only took {plays} tries to win!")
else:
print(f"Tried {plays} times, without pulling a winner. :(")
print(f"Your ticket: {new_ticket}")
print(f"Winning ticket: {winning_ticket}")
In Eric’s solution here:
def check_ticket(played_ticket, winning_ticket):
# Check all elements in the played ticket. If any are not in the
# winning ticket, return False.
for element in played_ticket:
if element not in winning_ticket:
return False
# We must have a winning ticket!
return True
I don’t understand the value of played_ticket or its purpose. Likewise the purpose of returning True.
Then secondly I see that in Eric’s solution he generates a ticket for himself in the following code:
def make_random_ticket(possibilities):
"""Return a random ticket from a set of possibilities."""
ticket = []
# We don't want to repeat numbers or letters, so we'll use a while loop.
while len(ticket) < 4:
pulled_item = choice(possibilities)
# Only add the pulled item to the ticket if it hasn't already
# been pulled.
if pulled_item not in ticket:
ticket.append(pulled_item)
return ticket
I’m wondering if I created a list called my_ticket with 4 numbers from ‘possibilities’, how would one run a loop that pulls numbers until it matches my_ticket? Instead of generating a ticket for myself.
Thank you.