[Tutor] another for loop question - latin square

Mitya Sirenef msirenef at lightbird.net
Mon Dec 31 01:49:23 CET 2012


On 12/30/2012 06:59 PM, Brandon Merritt wrote:
> I am having trouble figuring out a solution after a couple hours now 
> of playing with the code. I'm trying to make a latin square using the 
> code below:
>
> scaleorder = int(raw_input('Please enter a number for an n*n square: '))
>
>
> topleft = int(raw_input('Please enter the top left number for the 
> square: '))
>
> firstrow = range((topleft),scaleorder+1)
>
> count = 0
>
> while count < 8:
>     for i in firstrow:
>         print i
>         count += 1
>         firstrow[i+1]
>
>
> -----------------------------------------------------------------------
>
> It seemed like I could make the for loop work by doing something like 
> this:
>
> for i in firstrow:
>         print i, i+2


It's a bit hard to understand what you're trying to do, but if
my guess is anywhere close to truth, you might be trying
to make the first line of a latin square by incrementing
from a specified number. In this case, you should make a range
from topleft to scaleorder+topleft, and if you want the
top row randomized, you can use random.shuffle():

from random import shuffle

scaleorder = int(raw_input('Please enter a number for an n*n square: '))
topleft    = int(raw_input('Please enter the top left number for the 
square: '))
firstrow   = range(topleft, scaleorder+topleft)
shuffle(firstrow)

print firstrow


Does this help?   -m



-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/



More information about the Tutor mailing list