dimanche 14 mars 2021

Python, "if not in" statement confusion

I'm working on this lottery example and I'm just not grasping why the "if pulled_item not in winning ticket:" works. How is that if statement running if winning_ticket is just an empty list? What is the if statement checking then? I see we add pulled_item to winning ticket on the following line, but why can't we place the append line somewhere before the if statement?

possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'b', 'f', 'g', 'j', 'p']

winning_ticket = []

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)
        print(pulled_item)

Aucun commentaire:

Enregistrer un commentaire