# Multiplication table: what am i doing wrong?

Page 1 of 1

## 3 Replies - 2816 Views - Last Post: 01 March 2012 - 12:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=268916&amp;s=65498672a7df5120c09dc3faa7ca47a9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 greenpool

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 2202
• Posts: 3,400
• 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

### #3 greenpool

• New D.I.C Head

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

## Re: Multiplication table: what am i doing wrong?

Posted 29 February 2012 - 11:15 PM

sepp2k, 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

### #4 sepp2k

• D.I.C Lover

Reputation: 2202
• Posts: 3,400
• Joined: 21-June 11

## Re: Multiplication table: what am i doing wrong?

Posted 01 March 2012 - 12:25 AM

greenpool, on 01 March 2012 - 07:15 AM, said:

i needed to set col to 1 when row increments

Yeah, I somehow missed that in my first answer. Sorry about that.