mardi 3 août 2021

Is my while loop incorrectly comparing two lists?

My program randomly selects four items from the list, sorts them, and compares them to the list called 'my_ticket', which is also sorted.

My program is supposed to repeat selecting the four items until the four items it selects match the list 'my_ticket'.

Currently, the loop runs and never finds a match, if someone can point out the error(s) in my program I would appreciate it.

My program:

from random import choice
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']

# check if picks and my_ticket match
def check_if_equal(list_one, list_two):

    return sorted(str(list_one)) == sorted(str(list_two))

my_ticket = [1, 3, 'a', 'c']
picks = []
counter = 0

while check_if_equal(my_ticket, picks) == False:

    for pick in range(4):
        pick = choice(list)
        picks.append(pick)
        counter += 1
    print(picks)



Aucun commentaire:

Enregistrer un commentaire