Data Analysis - Python Programming Exercise

This Python program calculates various basic statistical operations such as sum, average, minimum, and maximum based on user input. The program prompts the user to enter a number and then updates the total sum, count, and recalculates the average, minimum, and maximum values. For example, when the user enters a number, the program updates and displays the running total, the count of numbers entered so far, the current average, and the minimum and maximum values. This exercise is ideal for practicing the use of loops, conditional statements, and basic arithmetic operations in Python. The program continues to prompt the user for numbers until the user enters 0, at which point it displays a summary of the statistics and terminates. By using a while loop, the program can repeatedly ask for input until the user decides to stop by entering 0. The program handles edge cases like the first input and updates the statistics accordingly. This is a practical example of how to manage and calculate basic statistics in Python while utilizing user input for dynamic results and conditional checks to control the flow of the program.

 Category

Mastering Flow Control

 Exercise

Data Analysis

 Objective

Develop a Python program to calculate various basic statistical operations: it will accept numbers from the user and display their sum, average, minimum, and maximum, as in the following example:

Number? 5
Total=5 Count=1 Average=5 Max=5 Min=5

Number? 2
Total=7 Count=2 Average=3.5 Max=5 Min=2

Number? 0
Goodbye!

(As seen in this example, the program will end when the user enters 0)

 Example Python Exercise

 Copy Python Code
# Initialize variables to store the sum, count, minimum, and maximum
total = 0
count = 0
minimum = None
maximum = None

# Use a while loop to prompt the user for numbers until 0 is entered
while True:
    num = float(input("Number? "))
    
    # Check if the entered number is 0
    if num == 0:
        print("Goodbye!")
        break
    
    # Update the total sum and count
    total += num
    count += 1
    
    # Update the minimum and maximum values
    if minimum is None or num < minimum:
        minimum = num
    if maximum is None or num > maximum:
        maximum = num
    
    # Calculate the average
    average = total / count
    
    # Display the current statistics
    print(f"Total={total} Count={count} Average={average} Max={maximum} Min={minimum}")

 Output

Number? 5
Total=5 Count=1 Average=5.0 Max=5.0 Min=5.0

Number? 2
Total=7 Count=2 Average=3.5 Max=5.0 Min=2.0

Number? 0
Goodbye!

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Ternary Operator, Positive & Lesser

    This Python program prompts the user to input two numbers and then uses the conditional operator (?) to perform a series of checks on the input. The program first che...

  •  Prime Digit

    This Python program prompts the user to input an integer and determines whether it is a prime number or not. A prime number is defined as a number great...

  •  Dispense Change

    This Python program calculates the change for a purchase, using the largest possible coins or bills. The program prompts the user for the price o...

  •  Error Handling

    This Python program prompts the user for two numbers and performs the division operation. It uses a try..except block to catch potential errors such as ...

  •  Positive and Negative Numbers

    In this exercise, you will develop a Python program that prompts the user to input a number and then determines if the number is positive or n...

  •  Multiply Unless Zero

    In this exercise, you will develop a Python program that prompts the user for a number. If the number is not zero, the program will ask for a second ...