Python Program for Number Input and Calculation

Answered using GPT-4o mini by AI Answer Generator

Question:

This program should start by printing a program header. The program then takes in 5 numbers from a user, one number at a time. After the 5th number has been input, display to the user the value of each number they have input, as well as the sum and average of all the numbers. Whenever asking a user for an input, you must provide prompts. (e.g., “Please enter a number:”) When you output the value for all numbers, it should show exactly 2 decimal places. Whenever you output a number, you must output strings that appropriately inform the user what they are looking at: (e.g., “The sum is:”) . To test your file, I will input 5 random numbers (they might be integers or floats) and examine the output. Below is the output of a program, tested using the values 1, 2, 3, 4, and 5.

Program 3 – Entering and Printing Numbers

================================ You will enter 5 numbers and then I will print them out with their sum and average.

Please enter a number: 5 Please enter a number: 4 Please enter a number: 3 Please enter a number: 2 Please enter a number: 1

You input the values: 5.00, 4.00, 3.00, 2.00, 1.00 The sum of the values you input is: 15.00 The average of the values you input is: 3.00

Answer:

Here is a Python program that meets the requirements you've specified:

# Program Header
print("#### Program 3 – Entering and Printing Numbers #####")
print("================================")
print("You will enter 5 numbers and then I will print them out with")
print("their sum and average.")
print("================================")

# Initialize an empty list to store the numbers
numbers = []

# Loop to get 5 numbers from the user
for i in range(5):
    number = float(input("Please enter a number: "))
    numbers.append(number)

# Calculate the sum and average
total_sum = sum(numbers)
average = total_sum / len(numbers)

# Output the results
print("================================")
print("You input the values: ", end="")
print(", ".join(f"{num:.2f}" for num in numbers))
print(f"The sum of the values you input is: {total_sum:.2f}")
print(f"The average of the values you input is: {average:.2f}")

Explanation:

  1. Program Header: The program starts by printing a header and a brief description of what it does.
  2. Input Loop: It uses a loop to prompt the user to enter 5 numbers, which are stored in a list.
  3. Calculations: After collecting the numbers, it calculates the sum and average.
  4. Output: Finally, it prints the numbers formatted to two decimal places, along with the sum and average, also formatted to two decimal places.

Example Usage:

When you run this program and input the numbers 1, 2, 3, 4, and 5, the output will be:

#### Program 3 – Entering and Printing Numbers #####
================================
You will enter 5 numbers and then I will print them out with
their sum and average.
================================
Please enter a number: 1
Please enter a number: 2
Please enter a number: 3
Please enter a number: 4
Please enter a number: 5
================================
You input the values: 1.00, 2.00, 3.00, 4.00, 5.00
The sum of the values you input is: 15.00
The average of the values you input is: 3.00

You can test the program with any set of 5 numbers, and it will provide the expected output.