Exercise
Positive And Negative Number Array
Objective
Develop a Python program to prompt the user for 10 real numbers and display the average of the positive ones and the average of the negative ones.
Example Python Exercise
Show Python Code
# Program developed by: Programmer 1, Programmer 2
# Initialize lists to store positive and negative numbers
positive_numbers = []
negative_numbers = []
# Prompt the user for 10 real numbers
for i in range(10):
number = float(input(f"Enter real number {i+1}: "))
if number > 0:
positive_numbers.append(number)
elif number < 0:
negative_numbers.append(number)
# Calculate and display the average of positive numbers
if positive_numbers:
positive_average = sum(positive_numbers) / len(positive_numbers)
print(f"The average of the positive numbers is: {positive_average}")
else:
print("No positive numbers entered.")
# Calculate and display the average of negative numbers
if negative_numbers:
negative_average = sum(negative_numbers) / len(negative_numbers)
print(f"The average of the negative numbers is: {negative_average}")
else:
print("No negative numbers entered.")
Output
Enter real number 1: 5.5
Enter real number 2: -3.2
Enter real number 3: 7.8
Enter real number 4: -4.5
Enter real number 5: 1.2
Enter real number 6: -2.3
Enter real number 7: 6.0
Enter real number 8: -8.9
Enter real number 9: -1.0
Enter real number 10: 2.4
The average of the positive numbers is: 4.18
The average of the negative numbers is: -4.18
Share this Python Exercise
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.
This Python program prompts the user to enter several numbers, calculating their sum as they go. The program continues to prompt the user for numbers until they enter...
This Python programming exercise is ideal for practicing the handling of multidimensional arrays. In this case, the goal is to prompt the user for the marks of 20 students, ...
This Python statistical program is a fantastic exercise for improving data handling and user interaction in programming. The program allows users to add new data...
This Python program is an excellent introduction to using NamedTuples for storing structured data. The program allows users to define 2D points with specific f...
In this exercise, you will develop a Python program that expands the previous exercise (NamedTuple point), allowing up to 1,000 points to be stored using an "array of...
In this exercise, you will develop a Python program that expands the previous exercise (array of NamedTuples), so that it displays a menu where the user...