Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is really hard to read. You should separate your problem into subproblems and structure them more logically.</p> <p>But to answer your question directly, in line 7 you're assigning an empty template to <code>self.__template</code>. In line 14 you're converting the template to a list of characters (why? after all you're not writing to it) and assign it to <code>self.__template_list</code>. Finally in lines 21 to 23 you're iterating over the list of template characters (which is still empty) and append it to <code>self.__answer</code>, which you print in <code>__str__()</code>. Thus you get just the pipes.</p> <p>Maybe I can give you some hints as to how to improve your code:</p> <ol> <li><p>The textual representation of the grid should be irrelevant to the general concept of the grid and thus should not concern most of the methods of your Grid class. In your case, it's littering the <code>__init__()</code> method and it makes it hard to understand what the method actually does. You could do several operations with your grid that don't need to know how the grid is displayed in the end (if at all).</p> <p>The code to output your grid should be completely confined to the method which is responsible for that, in your case <code>__str__()</code>.</p></li> <li><p>For variables that are irrelevant to other methods or the users of the class, use local variables instead of member variables. Unnecessary member variables make your code harder to understand, less efficient and confuse you when debugging, when inspecting the instance members with <code>dir()</code>, for instance.</p></li> <li><p>Think of a data structure that more logically represents your grid (and that contains only the necessary data, not superfluous details for representation). I suggest a list of lists, since that's very easy to manipulate in python (you could also use a two-dimensional numpy array, for instance).</p></li> </ol> <p>I suggest something akin to this:</p> <pre><code>class Grid: '''The grid for the soduku game.''' def __init__(self, puzzle): '''Constructs the soduku puzzle from the file.''' self.grid = [] with open(puzzle, "r") as f: for line in f: # strip CR/LF, replace . by space, make a list of chars self.grid.append([" " if char in " ." else char for char in line.rstrip("\r\n")]) def __str__(self): '''Prints the soduku puzzle nicely.''' lines = [] for i, row in enumerate(self.grid): if i != 0 and i % 3 == 0: # add a separator every 3 lines lines.append("+".join(["-" * 3] * 3)) # add a separator every 3 chars line = "|".join(map("".join, zip(*([iter(row)] * 3)))) lines.append(line) lines.append("") return "\n".join(lines) </code></pre> <p>Note that this version expects a file with a very rigid format (no separation lines or chars, exact number of chars per line). You can practise improving it to read more liberal formats.</p> <p>Also note that the only member variable I used is <code>self.grid</code>. All other variables are local to the respective function.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload