[Tutor] Making big 'uns into little 'uns

Ray Jones crawlzone at gmail.com
Thu Sep 6 17:33:57 CEST 2012


On 09/06/2012 08:29 AM, Walter Prins wrote:
> Hi Ray,
>
> On 6 September 2012 15:59, Ray Jones <crawlzone at gmail.com> wrote:
>> Basically it's as simple as ensuring that an array consists of integers,
>> and that those integers fall within a certain range. Rather than using
>> multiple 'if' statements, I was (am, at this point) using multiple tests
>> within a single 'if' statement. Nothing earth-shatteringly difficult,
>> but I was simply looking for a way to shorten the overall test
>> expression with a recursive(? is that the term) variable. No problem though.
> By array I suppose you mean "list of lists of items"?
Yes.
> Anyway, if you have such a structure, and you want to "visit" each in
> turn to check it, you can do this:
>
> for sublist in grid:
>     for item in sublist:
>         # code to check if "item" is in range goes here
>
> The above obviously doesn't actually track the "row" or "column"
> you're checking.  If you'd like to keep track of what "row"/"column"
> you're on, you can for example do:
>
> for row, sublist in enumerate(grid):
>     for col, item in enumerate(sublist):
>         # code to check if "item" is in range goes here
Our homework "monitor" complains if we use code that hasn't been
discussed in session yet. We haven't even progressed to 'range' in the
for loops yet - I don't think 'enumerate' would be appreciated. lol 
Technically, any variable pointer that I had managed to drum up here
would have been frowned upon, but I didn't figure it would have been a
serious breach.

I've got a pretty good handle on doing it the "hard way". I'm swatting
bugs right now... :))


Ray


More information about the Tutor mailing list