3 Replies - 2443 Views - Last Post: 01 March 2012 - 12:25 AM Rate Topic: -----

#1 greenpool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-November 11

Multiplication table: what am i doing wrong?

Posted 29 February 2012 - 10:27 PM

Hi guys,

Am working on multiplication program but i'm having trouble with it.

Here's my program:

def multiplication_table(n):
    
    row = 1
    col = 1
    
    while  row <= n:
        while col <= n:
            print 'row', ' * ', col, ' = ', row * col
            col = col +1
    row = row + 1
        


multiplication_table(2)
        



The output should look like this:

#1 * 1 = 1
#1 * 2 = 2
#2 * 1 = 2
#2 * 2 = 4


In my head the program logic makes sense but i don't get an output which suggests its broken loop of some sort but i'm so burnt out right now i can't seem to figure it out.

Any help is appreciated! thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication table: what am i doing wrong?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Multiplication table: what am i doing wrong?

Posted 29 February 2012 - 10:48 PM

Line 10 isn't indented enough. The way it is now, it is outside both of the loops, i.e. it won't execute until both loops have finished (i.e. never because the outer loop never finishes if line 10 doesn't execute).

Also the fact that you quoted 'row' when printing will mean that it will print as "row * 1 = 1" etc. You should remove the quotes around row.

This post has been edited by sepp2k: 29 February 2012 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 greenpool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-November 11

Re: Multiplication table: what am i doing wrong?

Posted 29 February 2012 - 11:15 PM

View Postsepp2k, on 29 February 2012 - 10:48 PM, said:

Line 10 isn't indented enough. The way it is now, it is outside both of the loops, i.e. it won't execute until both loops have finished (i.e. never because the outer loop never finishes if line 10 doesn't execute).

Also the fact that you quoted 'row' when printing will mean that it will print as "row * 1 = 1" etc. You should remove the quotes around row.


Thanks i fixed it:

def print_multiplication_table(n):
    
    row = 1
    col = 1
    
    while  row <= n:
        while col <= n:
            print row,' * ',col,' = ', row * col
            col = col +1
        row = row + 1
        


print_multiplication_table(2)


But my output looks like this:

1 * 1 = 1
1 * 2 = 2


I'm not sure why but it seems like its stopping before incrementing the value of row and going ahead with the loop. Wll either that or it is incrementing row but not executing it the 2nd time through the loop.

Any thoughts as to why?


Thanks.

I think i figured it out!

i needed to set col to 1 when row increments :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Multiplication table: what am i doing wrong?

Posted 01 March 2012 - 12:25 AM

View Postgreenpool, on 01 March 2012 - 07:15 AM, said:

i needed to set col to 1 when row increments :bigsmile:


Yeah, I somehow missed that in my first answer. Sorry about that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1