Python Forum
how to compare a list to a list of lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to compare a list to a list of lists (/thread-23323.html)



how to compare a list to a list of lists - kevthew - Dec-22-2019

Hi, I'm new to coding and trying to make a tic tac toe game. I'm almost finished but i cannot figure out how to create a win check mechanic... So far i have created a list of lists containing possible win values and also created 2 lists one for each player that save the value of there move after each move. My question is, how would one compare the items in a player list to see if any 3 combinations of moves is equal to any of the items in the wins list? Any help would be so much appreciated I've been stuck for days on this, I've posted a the code below Thanks In Advance

Also in the code below, keep in mind, the player list starts empty and only gets added to after the player move. So after first move i get an error saying "not enough arguments expected 3 received 1"
If i remove the j and k variables and just use a for i loop all i get is "no winner"
and, for the for i,j,k loop, i even tried starting the player lists with 2 none arguments to start but it still just prints no winner.

wins_list = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
#new_list = str(wins_list)  ##thought maybe a string would help but doesnt seem to work but also         
player1_peices = [1,4,6,2] ##example list for testing
player2_peices = [3,8,0]   ##example list for testing

##here after first move i get an error saying not enough arguments expected 3 recieved 1## 
##if i remove the j and k variables and just use a for i loop all i get is no winner ##
    for i,j,k in wins_list:
        if i in player1_pieces and j in player1_pieces and k in player1_pieces: 
            print("x wins")
                      
        elif i in player2_pieces and j in player2_pieces and k in player2_pieces: 
            print("o wins")
        else:
            print("no winner")
Again, thanks for any help.


RE: how to compare a list to a list of lists - ibreeden - Dec-22-2019

I get very different messages.
Error:
File "./forum11.py", line 9 for i,j,k in wins_list: ^ IndentationError: unexpected indent
When I corrected this I get:
Error:
Traceback (most recent call last): File "./forum11.py", line 10, in <module> if i in player1_pieces and j in player1_pieces and k in player1_pieces: NameError: name 'player1_pieces' is not defined
When I corrected that I get:
Output:
no winner no winner no winner no winner no winner no winner no winner x wins
The message you get assumes you have defined a function. I see no function definition in your code.