Learn Classes and Objects by Building a Sudoku Solver - Step 14

This topic has been explored and apparently solved but I’m still stuck. My code so far is below:

Your code so far

class Board:
    def __init__(self, board):
        self.board = board

    def __str__(self):
        upper_lines = f'\n╔═══{"╤═══"*2}{"╦═══"}{"╤═══"*2}{"╦═══"}{"╤═══"*2}╗\n'
        middle_lines = f'╟───{"┼───"*2}{"╫───"}{"┼───"*2}{"╫───"}{"┼───"*2}╢\n'
        lower_lines = f'╚═══{"╧═══"*2}{"╩═══"}{"╧═══"*2}{"╩═══"}{"╧═══"*2}╝\n'
        board_string = upper_lines

        for index, line in enumerate(self.board):
            row_list = []

/* User Editable Region */

            for square_no, part in enumerate([line[:3], line[3:6], line[6:]], start=1):
                for item in part:
                    item = '|'.join(part)

/* User Editable Region */

Can you please help me?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36 Edg/

Challenge Information:

Learn Classes and Objects by Building a Sudoku Solver - Step 14

Hello Bruno,

You’re now using a normal for loop, but this is kind of a throw back to the List Comprehensions we learned earlier.

So instead of a normal for loop, keep the '|'.join() but add the rest of the question inside. So you need to turn item in to a string, then loop using the loop they mentioned for item in part. I found this also very challanging so let me know how far you got and I’ll help you further if you didn’t get it after this.

Thank you for your help. I’ve tried these lines of code:
for square_no, part in enumerate([line[:3], line[3:6], line[6:]], start=1):
item = ‘|’.join(part) for item in part
part = str(item)
However it doesn’t pass.

Alright let me go step by step using the question (but ordered differently):

Use the join() method on the | character to join the elements of the segment (part).
You started on this by doing '|'.join(part) but instead of part we’re going to put the rest of the code inside.

For that, first, use a for loop for item in part to access all elements.
We’re going to use exactly what it says for item in part, inside the join method.

After that, convert each element to a string using str(item).
Although the question said “after that”, to make sure all items will become strings we can put it in front of the for statement we made to make sure that all items we are going to add to part will be strings, joined together by the join method.

Please post your code again once you tried this so I can see how it’s going.

Thanks, I was stuck on this part for a little while but I get what you’re saying now. You have to use str(item) inside the generator expression so that you turn each number into a string as you join it with ‘|’. I think it was the wording of the question that really threw me off.

Yeah makes sense, I had some difficulties here too.

       for square_no, part in enumerate([line[:3], line[3:6], line[6:]], start=1):
            str('|'.join(item for item in part))

Here’s my last code attempt, which failed.

Exactly as it looks. You need to convert the item produced by the for loop into a str in the exact literal syntax above.

Yeah, I haven’t realized what ‘item’ in the line of code that I was supposed to convert into a string. Thank you. This challenge is the hardest I’ve found in the whole course, a bit frustrating but exciting.

1 Like

Below mentioned code passed. I followed HungryBee’s explaination.


It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like