How to Print Table of a Given Number in Python
In this tutorial, we will learn how to program "How to Print the Table of a Given Number in Python." The objective is to correctly generate a table based on the input. This tutorial will guide you step by step through the entire process of safely generating a table of numbers. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and enhance your coding skills.
This topic is straightforward to understand. Just follow the instructions provided, and you will be able to complete it with ease. The program will guide you step by step through the process of generating a number table. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- while True:
- print("\n=============== Print Division Table of a Given Number ===============\n")
- try:
- n = float(input("Enter a number: "))
- except ValueError:
- print("Please enter a valid number.")
- continue
- print(f"\nDivision Table of {n}:\n")
- print("-" * 40)
- print(f"| {'Divisor':^10} | {'Expression':^15} | {'Result':^8} |")
- print("-" * 40)
- for i in range(1, 11):
- result = n / i
- print(f"| {i:^10} | {n} ÷ {i:^5} | {result:^8.2f} |")
- print("-" * 40)
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == 'no':
- print("Exiting program....")
- break
- elif opt != 'yes':
- print("Invalid input. Exiting program....")
- break
This Python program generates and displays a division table for a user-provided number. It operates inside an infinite loop, prompting the user to enter a number and handling invalid inputs gracefully using a try-except block. Once a valid number is entered, the program prints a neatly formatted table showing the division of the number by integers from 1 to 10, along with the result of each operation rounded to two decimal places. After displaying the table, the user is asked whether they want to try again. If the user inputs "no" or anything other than "yes", the program exits with a message; otherwise, it repeats the process.
Output:

There you have it we successfully created How to Print Table of a Given Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language