2

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.

5
  • 3
    Um... have you tried the equality operator? list1 == list2 Commented Sep 12, 2014 at 8:45
  • No I'm quite new to python what exactly does it do and how would I use it Commented Sep 12, 2014 at 8:47
  • 2
    This is not a python specific feature - it is a general programming operator that should exist in most (if not all) languages. Perhaps you should go back and run through some python tutorials to get a better foundation. Commented Sep 12, 2014 at 8:48
  • There is absolutely no way you'll be able to get anywhere near a working game without understanding basic comparison operators. Commented Sep 12, 2014 at 8:48
  • A problem here is that obviously your user will enter their attempt at the code using input() or raw_input() this returns a string and not a list so if you did list1 == users_list it 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 and appending them to their list, or you could get a bit more clever with it. Commented Sep 12, 2014 at 8:49

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

4 Comments

Note that if d = [3, 2, 1] then a == d will be False.
@PM2Ring Sure, those are different lists, they are equals sets.
Indeed. Sorry, I meant to direct that previous comment to the OP.
@PM2Ring No problem at all ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.