Introduction :
This Python program is designed to generate a multiplication table based on the input provided by the user. A multiplication table is a grid that displays the products of two integers from 1 to a specified range, typically 10. The program allows users to enter any number, and it will display the multiplication table for that number, showing its multiples up to 10.
Algorithm for the "Multiplication Table Generator" program:
- Start the program.
- Define the generate_multiplication_table function that takes one parameter number.
- Print a message to indicate that the multiplication table for the given number will be displayed.
- Start a for loop to iterate through the numbers 1 to 10 (inclusive) using the range(1, 11) Let the current iteration number be represented by the variable i.
- Within the loop, calculate the product of number and i and print a formatted string showing the multiplication operation and the result.
- End the loop.
- Start the main part of the program using the __name__ == “__main__”
- Try to execute the following code block within the try block:
- Prompt the user to input a number using the input function.
- Convert the user input to an integer using the int function and store it in the user_input variable.
- Call the generate_multiplication_table function, passing user_input as an argument.
- If the user enters a non-integer value, catch the ValueError exception within the except
- Print an error message to inform the user that they should enter a valid number.
- End the program.
The program aims to generate a multiplication table for a user-inputted number and display the results for the numbers 1 to 10. If the user enters a non-numeric value, the program will handle the exception gracefully and prompt the user to input a valid number.
Python Code for the "Multiplication Table Generator" program:
def generate_multiplication_table(number):
print(f"Multiplication Table for {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
if __name__ == "__main__":
try:
user_input = int(input("Enter a number to generate its multiplication table: "))
generate_multiplication_table(user_input)
except ValueError:
print("Invalid input! Please enter a valid number.")
Input :
Enter a number to generate its multiplication table: 9
Output :
Multiplication Table for 9:
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
Explanation :
- The generate_multiplication_table function takes a single parameter number, representing the input for which the multiplication table needs to be generated.
- Inside the function, it starts by printing a header line displaying the number for which the table is being generated.
- Then, it uses a for loop to iterate through the numbers 1 to 10 (inclusive) using the range(1, 11)
- During each iteration, the program calculates the product of the input number and the current iteration number i, and it prints a formatted string displaying the multiplication operation and its result.
- The main part of the program starts with a __name__ == “__main__” block, which ensures that the code inside it is only executed when the program is run directly, not when it’s imported as a module in another program.
- Inside the main block, the program tries to read user input using input(), and it converts the input into an integer using int().
- If the input provided by the user is a valid integer, it calls the generate_multiplication_table function, passing the user input as an argument.
- If the input is not a valid integer (e.g., if the user enters non-numeric characters), it catches the ValueError exception and displays an error message, prompting the user to enter a valid number.
Overall, this program provides a simple and interactive way for users to generate multiplication tables for any number they choose, making it useful for educational purposes or quick reference.