119,276 views
2 votes
2 votes
Print the two-dimensional list mult_table by row and column. Hint: Use nested loops.

Sample output with input: '1 2 3,2 4 6,3 6 9':
1 | 2 | 3
2 | 4 | 6
3 | 6 | 9
How can I delete that whitespace?

User AardvarkSoup
by
2.9k points

2 Answers

19 votes
19 votes

Answer:

count = 1

for row in mult_table:

for item in row:

if count < len(row):

count+=1

print(item,end=" | ")

else:

print(item)

count = 1

Explanation: Other answers didnt have the spacing correct

User Daneen
by
3.1k points
8 votes
8 votes

Answer:

The program is as follows:


mult\_table = [[1, 2, 3], [2, 4, 6], [3, 6, 9]]

count = 1

for row in mult_table:

for item in row:

if count < len(row):

count+=1

print(item,end="|")

else:

print(item)

count = 1

Step-by-step explanation:

This initializes the 2D list


mult\_table = [[1, 2, 3], [2, 4, 6], [3, 6, 9]]

This initializes the count of print-outs to 1

count = 1

This iterates through each rows

for row in mult_table:

This iterates through each element of the rows

for item in row:

If the number of print-outs is less than the number of rows

if count < len(row):

This prints the row element followed by |; then count is incremented by 1

count+=1

print(item,end="|")

If otherwise

else:

This prints the row element only

print(item)

This resets count to 1, after printing each row

count = 1

To delete the whitespace, you have to print each element as: print(item,end="|")

See that there is no space after "/"

User Gerd Klima
by
2.8k points