[Tutor] Creating Sudoku

W W srilyk at gmail.com
Mon Apr 7 14:52:55 CEST 2008


Actually, a dictionary might be a good idea.
I just played with this so youcan get a little picture.

>>> foo = {'1a': 3, '1b': 4}
>>> row = raw_input("Row: ")
Row: 1
>>> col = raw_input("Column: ")
Column: b
>>> val = raw_input("Value: ")
Value: 1
>>> foo[row+col] = val
>>> foo
{'1a': 3, '1b': '1'}

You could easily run int(val) to convert to the integer value to store
in the dict. The other benefit is that python ignores whitespace in
between dictionary elements, so you could do something to the effect
of

foo = {'1a': 1, '1b':2, '1c':3,
           '2a': 0, '2b': 9, '2c': 6}

Or something of similar nature.

I suppose in favor of a 2d list, the loop would be a little more
straighforward. With a dict, the pairs are in no particular order, so
you would have to basically create the same loop for a 2d array, and
then convert the values to strings.

Either way, good luck!
-Wayne

On 4/7/08, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> Tom Haynes wrote:
>  >
>  > G'day,
>  >
>  >
>  >
>  > I am trying to create a simple Sudoku game that takes a simple
>  > raw_input of a, r, c or something similar (where a = row, r = column,
>  > c = number to place there) and place it in a Sudoku game square. I
>  > have tried many times to start it from different angles and just can't
>  > do it. If someone could help step me through what needs to be done
>  > that would be greatly appreciated!
>  >
>
> Sounds like you need to have a 2-dimensional list to store your values.
>  The first and second indexes of the list would correspond to the
>  row/column variables you have, and the value would be the actual value
>  at each location.  You may want to initialize your list to some value
>  (fill it with something).  You could also use a dictionary, but it would
>  probably not be worthwhile.
>  You would also probably want to use the int(some_value) conversion on
>  your raw_inputs so you can use the values to index your list.
>  Let us know if this is not enough of a hint!  Hope it goes okay.
>  -Luke
>  _______________________________________________
>  Tutor maillist  -  Tutor at python.org
>  http://mail.python.org/mailman/listinfo/tutor
>


-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi


More information about the Tutor mailing list