[Tutor] Tkinter layout question

Phil phil_lor at bigpond.com
Sun Apr 23 20:50:47 EDT 2017


On Mon, 24 Apr 2017 09:24:55 +1000
Phil <phil_lor at bigpond.com> wrote:

> On Sun, 23 Apr 2017 09:39:54 +0200
> Sibylle Koczian <nulla.epistola at web.de> wrote:
> 
> > Am 20.04.2017 um 14:43 schrieb Alan Gauld via Tutor:
> > > Its not too bad you can map the large 9x9 table to the smaller
> > > units using divmod()
> > >
> > > So the 7th element becomes
> > > divmod(7) -> 2,1
> > >
> > 
> > Should be divmod(7, 3), shouldn't it?
> 
> Thanks Sibylle, I eventually stumbled upon the answer using my usual
> trial-and-error method. The 3, as in the number of cells, was the key.

Actually, that's not correct either.

Say I want the 7th cell in the first line of a 9 x 9 grid, that would be x = 7, y = 1. divmod(7,1) = 2,1 or the first cell in grid 3. So far so good.

Another example, x = 4, y = 3. divmod(4,3) = 1,1. What I need here is grid 2 x = 1 and y = 3.

Further complications are, arrays, or lists in Python, start a 0 and divmod may not be the answer because divide by 0 is not possible. Making adjustments for these two possibilities has resulted in complicated code that does give the desired result. Of course, I may have misunderstood the intention of Alan's mapping method.

So, what I need is a function to map from a 9 x 9 grid to a cell in a 3 x 3 grid.

My feeble simplified attempt is as follows:

def map_cell(x,y):
    return divmod(x,y)

while(True):
    x,y = input().split(" ")
    print (map_cell(int(x), int(y)))

-- 
Regards,
Phil


More information about the Tutor mailing list