Is This Python Multiplication Table Code Correct?

Click For Summary

Discussion Overview

The discussion revolves around a Python function intended to generate a multiplication table for numbers from 0 to 20. Participants explore the implementation details and structure of the code, focusing on how to format the output correctly.

Discussion Character

  • Technical explanation, Conceptual clarification, Homework-related

Main Points Raised

  • One participant shares a Python function that attempts to create a multiplication table using nested lists.
  • Another participant confirms that the function works as intended, indicating it produces the expected output.
  • A participant suggests modifying the function to format the output such that each row corresponds to the multiples of a specific integer, expressing uncertainty about the current structure.
  • A later reply identifies a potential issue with the placement of the list initialization for `Prod`, suggesting it should be inside the inner loop to achieve the desired row formatting.

Areas of Agreement / Disagreement

Participants generally agree that the function works, but there are differing views on how to structure the output for clarity. The discussion remains unresolved regarding the optimal implementation for the desired format.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

I want to write a function in Python that returns the multiplication table $20\times 20$.

We do that using lists in lists, right? I have written the following :

Code:
def mul_table() : 
    Prod = []  
    Table = [] 
    for i in range(21) : 
        for j in range(21) : 
            Prod.append(i*j) 
        Table.append(Prod)
    return Table 
    
print(mul_table())
Is that correct? :unsure:
 
Technology news on Phys.org
Hey mathmari!

It works for me. (Nod)
 
Klaas van Aarsen said:
It works for me. (Nod)

Could we make a change so that the result gives in different rows? I means all multiplers of 0 in the first row, in the second row all multiples of 1,etc? :unsure:
 
mathmari said:
Could we make a change so that the result gives in different rows? I means all multiplers of 0 in the first row, in the second row all multiples of 1,etc?
Ah, I see now that Prod = [] is in the wrong spot.
It should be inside the for-loop. (Worried)

Then the first row will contain only all multiples of 0. 🤔
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 55 ·
2
Replies
55
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K