
Python program that displays the multiplication table
Instructions
The purpose of this Python mini project is to write a Python program that displays the multiplication table. The program will prompt the user to enter a positive integer, validate the input, and then display the multiplication table from 1 to 10 for that number. It is Python challenge to practice loops lists and exceptions
- Write a Python program that prompts the user to enter a number.
- Validate the input to ensure it is a positive integer.
- Create a loop that iterates from 1 to 10.
- Within the loop, multiply the input number by the current iteration value.
- Display the multiplication table by printing the equation in the format “number x iteration = result”.
Steps
- Prompt the user to enter a number using the
"input()"function and store it in a variable. - Use a
"try-except"block to validate the input. If the input cannot be converted to a positive integer, display an error message and prompt the user again until a valid input is provided. - Convert the input to an integer using the
"int()"function and store it in a variable. - Create a
"for"loop that iterates over the range from 1 to 11. - Within the loop, calculate the result by multiplying the input number with the iteration value and store it in a variable.
- Print the multiplication table by using the
"print()"function to display the equation in the format
“number x iteration = result”.
Python Mini Project source code
while True:
try:
number = int(input("Enter a positive integer: "))
if number <= 0:
raise ValueError
break
except ValueError:
print("Invalid input. Please enter a positive integer.")
for i in range(1, 11):
result = number * i
print(f"{number} x {i} = {result}")
Expected Output
Enter a positive integer: 9 ## Output:
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 I you wish to keep practicing Python, check here for more Python project ideas.