Articles

Number Analysis Program Python [Solved]

8 min read
Number Analysis Program Python

In this article, we will learn how to write a Python number analysis program. We will create a number analysis program Python that prompts the user to enter a list of numbers and then displays the lowest number, highest number, sum of the numbers, and average of the numbers. We will see two most frequently asked number analysis Python homework.

Frequently Asked Python Number Analysis Program

Q1. Write a Python program that asks the user to enter a series of 20 numbers. The program should store the number in a list. It should have the following functions

  • find_lowest(list): finds and returns the lowest number in the list. Do not use the main function.
  • find_highest(list): find and returns the highest number in the list. Do not use the max function.
  • calculate_total(list): calculates and returns the total of the numbers in the list.

The program should call the functions mentioned above and display the lowest, highest and total values in the list. It should also calculate and display the average of the numbers in the list.

Q2. Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in a list and then display the following data:

  1. The lowest number in the list
  2. The highest number in the list
  3. The total of the numbers in the list
  4. The average of the numbers in the list

Above are two questions that are often asked as Python homework help from experts. However, both the question is same and the solution is as below-

In this example, we need the user to enter a list of 20 numbers, and the program will analyze the numbers like before. However, we will not be using the min and max inbuilt functions; we will write the function ourselves.

Solved Python Number Analysis Program Q1 &Q2

We start by defining four functions: find_lowest(), find_highest(), calculate_total(), and calculate_average().

These functions will be used to find the lowest number, highest number, total sum of the list of numbers, and the average of the list respectively.

Here is the code:

def find_lowest(lst):
    lowest = lst[0]
    for num in lst:
        if num  highest:
            highest = num
    return highest

def calculate_total(lst):
    total = 0
    for num in lst:
        total += num
    return total

def calculate_average(lst):
    total = calculate_total(lst)
    average = total / len(lst)
    return average

Next, we define the get_input() function, which handles user input for the list of numbers.

It takes care of getting the user input and handling invalid input and keyboard interrupts. get_input() function takes all inputs as a single string and parses them by splitting the space separator.

If a keyboard interrupt is detected at any point during the program, the script will terminate immediately with an appropriate message.

Here is the code:

def get_input():
    while True:
        try:
            numbers = input("Enter 20 numbers separated by spaces: ").split()
            numbers = [float(num) for num in numbers]
            if len(numbers) != 20:
                raise ValueError
            return numbers
        except ValueError:
            print("Invalid input. Please enter exactly 20 valid numbers separated by spaces.")
        except KeyboardInterrupt as e:
            raise e

The function prompts the user to enter all 20 numbers separated by spaces as a single input string.

It then splits the input string by a space separator to obtain a list of number strings.

Then it converts each number string to a float and returns the resulting list of numbers.

The function also handles invalid input and keyboard interrupts.

Finally, we use these functions to analyze the list of numbers entered by the user.

try:
    # Ask user to input 20 numbers
    numbers = get_input()

    # Call functions to find lowest, highest, and total
    lowest = find_lowest(numbers)
    highest = find_highest(numbers)
    total = calculate_total(numbers)
    average = calculate_average(numbers)

    # Display results
    print(f"Lowest number: {lowest}")
    print(f"Highest number: {highest}")
    print(f"Total: {total}")
    print(f"Average: {average}")

except KeyboardInterrupt:
    print("\nProgram terminated by user.")

Here is the complete code:


def find_lowest(lst):
    lowest = lst[0]
    for num in lst:
        if num  highest:
            highest = num
    return highest

def calculate_total(lst):
    total = 0
    for num in lst:
        total += num
    return total

def calculate_average(lst):
    total = calculate_total(lst)
    average = total / len(lst)
    return average

def get_input():
    while True:
        try:
            numbers = input("Enter 20 numbers separated by spaces: ").split()
            numbers = [float(num) for num in numbers]
            if len(numbers) != 20:
                raise ValueError
            return numbers
        except ValueError:
            print("Invalid input. Please enter exactly 20 valid numbers separated by spaces.")
        except KeyboardInterrupt as e:
            raise e

try:
    # Ask user to input 20 numbers
    numbers = get_input()

    # Call functions to find lowest, highest, and total
    lowest = find_lowest(numbers)
    highest = find_highest(numbers)
    total = calculate_total(numbers)
    average = calculate_average(numbers)

    # Display results
    print(f"Lowest number: {lowest}")
    print(f"Highest number: {highest}")
    print(f"Total: {total}")
    print(f"Average: {average}")

except KeyboardInterrupt:
    print("\nProgram terminated by user.")

If you want to explore more about the number analysis program Python. Then below is an in-depth tutorial on Python number analysis.

Getting Started with Number Analysis Python

To get started, we will first write a program that prompts the user to enter a list of numbers. We will use the input function to get the list of numbers from the user.

Here’s the code for it:

numbers = input("Enter a list of numbers separated by a space: ")

This code will prompt the user to enter a list of numbers separated by a space.

The numbers entered by the user will be stored in the numbers variable as a string.

Converting the String into a List in Python

Since we need to perform operations on the individual numbers in the list, we need to convert string into a list of integers.

We will use the split function to split the string into a list and then use the map function to convert each string element into an integer.

Here’s the code to convert string to list in Python:

numbers_list = list(map(int, numbers.split()))

This code will split the numbers string into a list of strings, and then the map function will convert each string element into an integer.

Finally, the list function will convert the map object into a list of integers.

Finding the Lowest Number in Number Analysis Program

To find the lowest number in the list, we will use the min function.

The min function takes a list as an argument and returns the lowest value in the list. Here’s the code for it:

lowest_number = min(numbers_list)

This code will find the lowest number in the numbers_list list and store it in the lowest_number variable.

Finding the Highest Number in Python Number Analysis Program

Similarly, to find the highest number in the list, we will use the max function. The max function takes a list as an argument and returns the highest value in the list.

Here’s the code for it:

highest_number = max(numbers_list)

This code will find the highest number in the numbers_list list and store it in the highest_number variable.

Finding the Sum of Numbers in Number Analysis Program Python

To find the sum of the numbers in the list, we will use the sum function. The sum function takes a list as an argument and returns the sum of all the values in the list.

Here’s the code for it:

sum_of_numbers = sum(numbers_list)

This code will find the sum of all the numbers in the numbers_list list and store it in the sum_of_numbers variable.

Finding the Average of Numbers in Number Analysis Program Python

To find the average of the numbers in the list, we will divide the sum of the numbers by the total number of numbers in the list.

Here’s the code for it:

average_of_numbers = sum_of_numbers / len(numbers_list)

This code will find the average of all the numbers in the numbers_list list and store it in the average_of_numbers variable.

Printing the Results

Finally, we will print the results using the print function.

print("Lowest Number:", lowest_number)
print("Highest Number:", highest_number)
print("Sum of Numbers:", sum_of_numbers)
print("Average of Numbers:", average_of_numbers)

Code solution for Number Analysis Program in Python

To summarize, the Python program that reads a list of numbers from the user and displays the lowest number, the highest number, the sum of numbers, and the average of the numbers is a simple and useful tool that can be used for data analysis and statistics.

Now, let’s take a look at the complete code of the program:

numbers = input("Enter a list of numbers separated by a space: ")
numbers_list = list(map(int, numbers.split()))
lowest_number = min(numbers_list)
highest_number = max(numbers_list)
sum_of_numbers = sum(numbers_list)
average_of_numbers = sum_of_numbers / len(numbers_list)
print("Lowest Number:", lowest_number)
print("Highest Number:", highest_number)
print("Sum of Numbers:", sum_of_numbers)
print("Average of Numbers:", average_of_numbers)

Simply run the code and follow the instructions to enter the list of numbers that you want to analyze. The program will then display the lowest number, highest number, sum of the numbers, and average of the numbers in the list.

Conclusion

In conclusion, we have shown two examples of how to solve a Python number analysis program.  It allows users to input a list of numbers and displays essential statistics such as the lowest number, highest number, sum of the numbers, and average of the numbers.

This program can be particularly useful in various fields such as data analysis and statistics, where analyzing lists of numbers is a common task.

Overall, this number analysis program Python provides a solid foundation for beginners to start learning the language and its fundamental concepts.