pythonreturnwhy3introsort

why do this defition in textbook return 2 values?


In this post I will do 3 things in order:

A introduce the question

B display the answer in the textbook to this question

C show my doubt


A introduce the question

The question goes like this:


(1) Make a list or tuple containing a series of 10 numbers and five letters. Randomly select four numbers or letters from the list and print a message saying that any ticket matching these four numbers or letters wins a prize;

(2) Make a list or tuple containing a series of 10 numbers and five letters. Randomly select four numbers or letters from the list and print a message saying that any ticket matching these four numbers or letters wins a prize.


B display the answer in the textbook to this question

This answer code is from the textbook:


(1)

from random import choice

possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']

winning_ticket = []
print("Let's see what the winning ticket is...")

# 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:
        print(f"  We pulled a {pulled_item}!")
        winning_ticket.append(pulled_item)

(2)

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 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

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

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}")

print(check_ticket(new_ticket, winning_ticket))


C Show my doubt And my question lies in this para:

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}")

I wonder why it first define won=False,then in the if not won (why doesn't it say if True?) it defines won = check_ticket(new_ticket,winning_ticket) which can return False or True, and at the end says if won??? is this mean if False? why doesn't it say so?

And moreover,I wonder why my code to this question connot work:



from random import choice

possibilities=['1','2','3','4','5','6','7','8','9','10','a','b','c','d','e']

def make_winning_ticket(possibilities):
    winning_ticket=[]
    while len(winning_ticket)<4:
        new_number=choice(possibilities)
        if new_number not in winning_ticket:
          winning_ticket.append(new_number)
    return winning_ticket

def make_new_ticket(possibilities):
     new_ticket=[]
     while len(new_ticket)<4:
        new_number=choice(possibilities)
        if new_number not in new_ticket:
          new_ticket.append(new_number)
     return new_ticket

play=0
max_try=100000

def check_ticket(new_ticket,winning_ticket):
    while play<max_try:
        for element in new_ticket:
            if new_ticket not in winning_ticket:
             play+=1
             make_new_ticket(possibilities)
             
    print(play)
 


make_winning_ticket(possibilities)
make_new_ticket(possibilities)
check_ticket(winning_ticket,new_ticket)

Thank you for your patience:)


Solution

  • I wonder why it first define won=False, then in the if not won (why doesn't it say if True?) it defines won = check_ticket(new_ticket,winning_ticket) which can return False or True, and at the end says if won??? is this mean if False? why doesn't it say so?

    No. It means what it says:

    And moreover,I wonder why my code to this question connot work:

    To be honest, I didn't try to follow each line of it, but what seems to be one of the biggest problems: you discard the return values of your functions.

    If we have a function, say

    def f(x):
        y = 2 * x
        return y
    

    it is not sufficient to do

    f(10)
    

    and then to expect that we have done something useful: the y variable is completely inside the function. In order to get access to its value, we must do

    y = f(10) # y will be 20
    

    or even (with a completely different name than inside the function):

    y1 = f(10) # y1 will be 20
    y2 = f(15) # y2 will be 30
    

    So in your example, you should do at least

    winning_ticket = make_winning_ticket(possibilities)
    new_ticket = make_new_ticket(possibilities)
    check_ticket(winning_ticket, new_ticket)