Python Forum
how to compare a list to a list of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to compare a list to a list of lists
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,563 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,499 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Compare two lists (with intersections). wnesbv 0 954 Jul-06-2022, 09:07 AM
Last Post: wnesbv
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020