Technology
How to Develop a Python Program to Input 10 Numbers and Output the 3 Largest Numbers
How to Develop a Python Program to Input 10 Numbers and Output the 3 Largest Numbers
Introduction
In this guide, we will walk through the process of creating a Python program that inputs 10 numbers and outputs the three largest numbers using a straightforward and efficient method. This tutorial is designed for beginners interested in Python programming and covers the essential steps required to achieve this functionality.Step-by-Step Guide
To develop a Python program that inputs 10 numbers and outputs the three largest numbers, follow the steps below:
Step 1: Initialize an Empty List to Store the Numbers
First, we need to initialize an empty list to store the input numbers.
numbers []
Step 2: Input 10 Numbers
Next, we will use a loop to gather 10 numbers from the user. Python's `for` loop is a convenient way to handle this task.
for i in range(10): num float(input("Enter a number: ")) (num)
Step 3: Sort the Numbers in Descending Order
The `sort()` method in Python can be used to sort the list in descending order with the `reverseTrue` parameter.
(reverseTrue)
Step 4: Output the Top Three Numbers
Finally, we need to retrieve the top three numbers from the sorted list and print them.
print("The three largest numbers are:", numbers[:3])
Alternative Approach: Using Bubble Sort
If you prefer a different sorting method, such as the Bubble Sort algorithm, you can implement the following steps:
Step 1: Read 10 Numbers into an Array
numbers []for i in range(10): num float(input("Enter a number: ")) (num)
Step 2: Sort the Array Using Bubble Sort
Bubble Sort is a simple comparison-based algorithm. Here is a Python implementation of Bubble Sort to sort the array in descending order.
def bubble_sort_descending(arr): n len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j]
Step 3: Print the Last Three Elements of the Array
print("The three largest numbers are:", numbers[-3:])
Conclusion
This guide has demonstrated how to create a Python program that inputs 10 numbers and outputs the three largest numbers using both an efficient built-in sorting method and a basic sorting algorithm. Whether you want a simple solution using Python's built-in functions or a more traditional approach like Bubble Sort, both methods work effectively.