Home > Blockchain >  Python Function - Math Tables
Python Function - Math Tables

Time:12-05

**Create a math tables Python function **

print out nicely formatted arithmetic tables (for addition and multiplication) Function definition:def mathTables() Expected output: no return value; prints out and X table for values 1 to 10

I am very new to Python and really struggling with this one. I have the multiplication table finished, but was helped by a tutor. A better explanation on that code would be amazing as well. I'm stuck on where to start for the addition table. An explanation or simple code would be super helpful. I'm not too familiar with formatted code so the simpler the better. Thank you!!

def mathTables():

    for i in range(1, 10 1):
     for j in range(i, (i*10) 1):
          if (j % i == 0):
              print(j, end="\t")
     print()
     

I've tried to write the code myself, however I cant figure out how to stack the numbers on top of each other to form a table

CodePudding user response:

Here is a simple way to create a multiplication table in Python using a nested for loop. The outer loop iterates over the numbers 1 through 10, and the inner loop iterates over the numbers 1 through 10 for each number in the outer loop. For each iteration of the inner loop, we print the product of the outer loop variable and the inner loop variable. We also use the end parameter of the print() function to specify that we want to print each value on the same line with a tab character between them.

Here is the code:

def mathTables():
  # Print the multiplication table
  print("Multiplication Table")
  for i in range(1, 10 1):
    for j in range(1, 10 1):
      # Print the product of i and j
      print(i * j, end="\t")
    # Start a new line after each row of the table
    print()

mathTables()

This will print the multiplication table with the values 1 through 10 on the x- and y-axes.

To create an addition table, you can use the same approach but change the print statement to print the sum of the outer loop variable and the inner loop variable instead of the product. Here is the code:

def mathTables():
  # Print the addition table
  print("Addition Table")
  for i in range(1, 10 1):
    for j in range(1, 10 1):
      # Print the sum of i and j
      print(i   j, end="\t")
    # Start a new line after each row of the table
    print()

mathTables()

In the mathTables() function, we first print a message indicating that we are printing a multiplication table. Then, we have a nested for loop to iterate over the numbers 1 through 10. The outer for loop has a variable i that represents the current number in the outer loop, and the inner for loop has a variable j that represents the current number in the inner loop.

For each iteration of the inner loop, we print the product of i and j. We also use the end parameter of the print() function to specify that we want to print each value on the same line with a tab character between them. This way, we can print all the values in a row of the table on the same line.

After each row of the table has been printed, we start a new line by calling the print() function without any arguments. This way, each row of the table will be printed on a separate line.

Finally, we call the mathTables() function to execute the code and print the multiplication table.

This will print the addition table with the values 1 through 10 on the x- and y-axes.

I hope this helps! Let me know if you have any other questions.

CodePudding user response:

Partial answer: Code Explanation

The code formats the table using an escape code \t and a newline. \t is an sequence that automatically inserts a tab character, which tabulates the next character to the next tab stop. In simpler terms, it adds spaces until the next character is at a predefined point. The newline is accomplished with a simple print() statement.

In the rest of the program, we have two loops and an if statement. The first loop will execute 10 times and generates the row number that goes from 1 to 10, inclusive. The second loop generates a variable j that goes from i, the row number, to the end of the scope of the row. Since our range is 1~10, the highest number for each row would be the row number times 10. The if statement will execute every time j is a multiple of the row number.

  • Related