Is This Python Multiplication Table Code Correct?

Click For Summary
SUMMARY

The Python function provided for generating a multiplication table from 0 to 20 contains a logical error in its implementation. The variable Prod should be initialized within the inner loop to ensure that each row of the multiplication table is correctly populated with the respective multiples. The corrected version of the function will yield a 2D list where each sublist represents the multiples of a specific integer from 0 to 20.

PREREQUISITES
  • Understanding of Python programming language
  • Familiarity with list data structures in Python
  • Basic knowledge of nested loops in programming
  • Concept of multiplication tables
NEXT STEPS
  • Review Python list comprehension for more concise code
  • Explore Python functions and their scope
  • Learn about 2D arrays in Python using NumPy
  • Investigate debugging techniques for Python code
USEFUL FOR

Python developers, educators teaching programming concepts, and anyone interested in generating mathematical tables programmatically.

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
4K
  • · 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