Issue
Print the 2-dimensional list mult_table by row and column. Using nested loops. Sample output for the given program(without spacing between each row):
1 | 2 | 3
2 | 4 | 6
3 | 6 | 9
This is my code: I tried using a nested loop but I have my output at the bottom. It has the extra | at the end
for row in mult_table:
for cell in row:
print(cell,end=' | ' )
print()
1 | 2 | 3 |
2 | 4 | 6 |
3 | 6 | 9 |
Solution
Try
for row in mult_table:
print(" | ".join([str(cell) for cell in row]))
The join()
joins the given elements into one string, using " | "
as the separator. So for three in a row
, it only uses two separators.
Answered By - e0k
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.