[Tutor] For Loops and nested loops

bob gailer bgailer at gmail.com
Sat Aug 16 16:27:17 CEST 2008


Umesh Singhal wrote:
> Hi im still relatively new to python and i am designing a 
> multiplication table that enables a user to input the size of the 
> times table unfortunately ive stumbled on the nested loops this is 
> what i have right now:
>
> a=raw_input('please enter a number')
> b=int(a)
> n=b+1
> for row in range(1, n):
>      for col in range(1, n):
>              print "%3d " % (row * col),
>      print
>
> the input which comes out is:
>   1    2    3    4    5
>   2    4    6    8   10
>   3    6    9   12   15
>   4    8   12   16   20
>   5   10   15   20   25
>
> however i need something more on the lines of:
> | 2 3 4 5
> ==================
> 2 | 4 6 8 10
> 3 | 6 9 12 15
> 4 | 8 12 16 20
> 5 | 10 15 20 25
>
> does anyone know what i need the third nested for loop to be ? to make 
> it like the above example. Pleasee help me !!

I could write the program for you but I'd rather prod you into working 
it out yourself as I think you will learn more in the process.

What code would you write to generate the first line? It is a loop, but 
not nested.

At the beginning of each row you want to print the row number. What one 
statement would you add and where would you put it to do that.

BTW I would find your questions and comments easier to read if you 
divided sentences with capitalization and punctuation.

Be sure to reply-all.

-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?



More information about the Tutor mailing list