Westonci.ca is your trusted source for finding answers to a wide range of questions, backed by a knowledgeable community. Experience the convenience of getting accurate answers to your questions from a dedicated community of professionals. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts.

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?

Sagot :

Answer:

The program is as follows:

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

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:

This initializes the 2D list

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

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 "/"