I'm developing a code breaker game where the user is given a coded list and the user has to guess what letter each symbol represents. When the user thinks he has replaced all of the symbols with the correct letters he/she would then type 'check'. What I want my check function to do, is to compare the users list with a separate list with the correct answers but I am stuck on how to do so.
1 Answer
Just use the == operator, it calls the method __eq__ on the list which check the elements equality:
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> c = [1, 2, 3, 4]
>>> a == b
True
>>> a == c
False
4 Comments
PM 2Ring
Note that if
d = [3, 2, 1] then a == d will be False.enrico.bacis
@PM2Ring Sure, those are different lists, they are equals sets.
PM 2Ring
Indeed. Sorry, I meant to direct that previous comment to the OP.
enrico.bacis
@PM2Ring No problem at all ;)
list1 == list2input()orraw_input()this returns astringand not alistso if you didlist1 == users_listit wouldn't work as one is a list, one is a string. You could get around this by getting the user to enter their numbers 1 by 1 andappending them to their list, or you could get a bit more clever with it.