Python multiplication table | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python multiplication table

Write code that will output a multiplication table for 10 positive numbers across the columns and 10 positive integers down the rows. Prompt the user for the initial values for the columns and rows. row = int(raw_input("Enter the first row number: " )) col = int(raw_input("Enter the frist column number: ")) lastRow = row + 10 lastCol = col + 10 while (row < lastRow): print "%4d" % (col * row) while(col < lastCol): print "%4d" % (col * row), col += 1 print "%4d" % (col * row) row += 1 My effort followed some explanation from this video (https://www.scaler.com/topics/multiplication-table-in-JUMP_LINK__&&__python__&&__JUMP_LINK/) I saw, which doesn't print how Id expect it. What am I supposed to call the print statements, and what is wrong with the iterations? Here's a second attempt, which is better but not what I expected. row = int(raw_input("Enter the first row number: " )) col = int(raw_input("Enter the frist column number: ")) lastRow = row + 10 lastCol = col + 10 x=row y=col while (x < lastRow): while(y < lastCol): y += 1 print "%4d" % (y * x) x += 1 Sorry for the repeated post; I had no idea it was poor etiquette.

15th Dec 2022, 10:41 AM
sarthak jain
sarthak jain - avatar
1 Answer
+ 2
The real problem is you forget to reset y to the start value after the inner while loop is finished. Your while loops should look something like this (python 3 syntax): while (x < lastRow): while(y < lastCol): print ("%3d" % (y * x), end = '') y += 1 print() y = col x += 1
15th Dec 2022, 12:22 PM
Paul
Paul - avatar