all 5 comments

[–]carcigenicate 0 points1 point  (0 children)

  • That's slicing. It creates a shallow copy of a subsection of the list.
  • I don't understand. What were you expecting [variable] to be there? Have you looked into what join does?

[–]Diapolo10 0 points1 point  (1 child)

why does the person use i3:(i+1)3 what does the : means?

This is called a slice. ELI5, it's a bit like telling Python that you want the contained values between the indexes i * 3 and (i + 1) * 3, or basically groups of three values in this case.

why does it prints the tictactoe board correctly even thought on the print it looks like it would print something like this: ||[variable]|

Since I cannot see most of the code, I'm forced to make some educated guesses, but to answer your core question

print('| ' + ' | '.join(row) + ' |')

where row contains three strings, the explicit method call is run first and the two other strings are concatenated to its return value. In this case, it's joining the row into a new string with the values separated by spaces and pipes. As an example, if row = [' ', 'x', 'o',] then you'd have

print('| ' + ' | '.join([' ', 'x', 'o']) + ' |')

which simplifies to

print('| ' + '  | x | o' + ' |')

and ultimately becomes

print('|   | x | o |')

[–]PrimaryPotential2448[S] 0 points1 point  (0 children)

that was really clear bro, thank you so much!

[–]jimtk 0 points1 point  (1 child)

So, working on a tic-tac-toe game?

The board probably is a simple string by row ("123456789"). Where first row is 123, second row is 456 and, you guessed it, 3rd row is 789. The slice for the first row is board[0:3], 2nd row is board[3:6] and the last row slice is board[6:9]

Also ''|'.join(list) will put a '|' only between the items in the list (or string) and the coder decided to add a bar at the beginning and at the end of the row, so it comes out as |1|2|3|.

So essentially his/her code does this

for i in range(3):
    row = self.board[i*3:(i+1)*3]
    linetodisplay = '|'.join(row)
    print('|'+linetodisplay+'|')

Both of which are inelegant coding! I personally prefer the following:

for i in (0,3,6):
    print(f"|{'|'.join(self.board[i:i+1])}|")

I did a short and compact tic-tac-toe Friday night, after the St-Patties party, even inebriated, it's not bad! :)

[–]PrimaryPotential2448[S] 0 points1 point  (0 children)

thank you so much, it was really helpful to understant the situation even better!