a) Write a function that produces a table of the numbers from 1 to 15, their squares and
their cubes; Make sure your function is called from main (). You have to test whether function passes various data on it.
SAMPLE OUTPUT IN THE PICTURE:
Share
Python
def print_table(start, end):
def print_table(start, end): print("Number | Square | Cube")
def print_table(start, end): print("Number | Square | Cube") print("----------------------")
def print_table(start, end): print("Number | Square | Cube") print("----------------------") for i in range(start, end+1):
def print_table(start, end): print("Number | Square | Cube") print("----------------------") for i in range(start, end+1): print("{:6d} | {:6d} | {:6d}".format(i, i*i, i*i*i))
# call the function with a range of 1 to 15
# call the function with a range of 1 to 15print_table(1, 15)
This code defines a function print_table that takes two arguments, start and end, which specify the range of numbers to include in the table. The function then loops through the range of numbers, computes their squares and cubes, and prints a formatted table with three columns for the number, its square, and its cube.
To test the function with different ranges of numbers, you can simply call the function with different arguments, like this:
# call the function with a range of 10 to 20
# call the function with a range of 10 to 20print_table(10, 20)
# call the function with a range of 10 to 20print_table(10, 20)# call the function with a range of -5 to 5
# call the function with a range of 10 to 20print_table(10, 20)# call the function with a range of -5 to 5print_table(-5, 5)
This will print tables for the ranges 10 to 20 and -5 to 5, respectively.