Printing a multiplication table with nested loops?
Low-Level: Your code shows two '|'
after each number because that's what you're telling it to do (one of them is passed to print()
right after num
, and one of them is added by the use of end='|'
, which tells print()
to add a '|'
at the end of the printed string).
Using end=''
avoids the second '|'
, but you still end up with a pipe character at the end of each row from the print()
call.
To get '|'
between items, but not at the end of the row, you need to handle the last item in each row specially (or, better use " | ".join(row)
to add the separators automatically).
High-Level: You're likely not solving this problem the way it was intended to be solved.
Your answer hard-codes the multiplication table, but you could be generating it as part of the loop, like:
# Store the size of the table here
# So it's easy to change later
maximum = 3
# Make a row for each y value in [1, maximum]
for y in range(1, maximum + 1):
# Print all x * y except the last one, with a separator afterwards and no newline
for x in range(1, maximum):
print(x * y, end=' | ')
# Print the last product, without a separator, and with a newline
print(y * maximum)
This solves the specific problem given, but we can now change the value of "maximum" to also generate a square multiplication table of any size, and we don't have to worry about errors in our multiplication table.
How can a multiplication table be displayed using only nested for loops and System.out.println in Java?
This is something that you will learn in any beginners book:
for (int i=1;i<=10;i++){
for (int j=1;j<=10;j++)
System.out.print("\t"+i*j);
System.out.println();
}
Multiplication Table using Nested Loops
How to print multiplication table using nested for loops
One of the simplest methods would be to start counting from 0 instead of 1:
for row in range(0, 10):
for col in range(0, 10):
num = row * col
if num < 10:
empty = " "
else:
if num < 100:
empty = " "
if col == 0:
if row == 0:
print(" ", end = '')
else:
print(" ", row, end='')
elif row == 0:
print(" ", col, end='')
else:
print(empty, num, end = '')
print()
this will print
1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10 12 14 16 18
3 3 6 9 12 15 18 21 24 27
4 4 8 12 16 20 24 28 32 36
5 5 10 15 20 25 30 35 40 45
6 6 12 18 24 30 36 42 48 54
7 7 14 21 28 35 42 49 56 63
8 8 16 24 32 40 48 56 64 72
9 9 18 27 36 45 54 63 72 81
That being said, here is a one-liner version that results in the same output:
print("\n".join(("{:>4}"*10).format(*(i*j if i*j else i+j if i+j else "" for j in range(10))) for i in range(10)))
Related Topics
How to Put a Space Between Two String Items in Python
How to Mention a User in Discord.Py
Using Python, How to Access a Shared Folder on Windows Network
Python Pandas - Get Row Based on Previous Row Value
How to Drop Rows from Pandas Data Frame That Contains a Particular String in a Particular Column
Removing Backslashes from a String in Python
How to Limit the User Input to Only Integers in Python
How to Kill a While Loop With a Keystroke
How to Determine If My Python Shell Is Executing in 32Bit or 64Bit
How to Make a Function Change Variables While in a While Loop
Combine Date and Time Columns Using Python Pandas
Python Db-Api: Fetchone VS Fetchmany VS Fetchall
Key Error When Selecting Columns in Pandas Dataframe After Read_Csv
Remove White Space from Entire Dataframe