Simple Python program to read 'n' numbers from users and calculate the average of those numbers.
def calculate_average(numbers):
return sum(numbers) / len(numbers) if numbers else 0
def main():
# Read the number of values
n = int(input("Enter the number of values: "))
if n <= 0:
print("The number of values should be greater than 0.")
return
numbers = []
# Read n numbers from the user
for i in range(n):
while True:
try:
number = float(input(f"Enter number {i + 1}: "))
numbers.append(number)
break
except ValueError:
print("Invalid input. Please enter a valid number.")
# Calculate and display the average
average = calculate_average(numbers)
print(f"The average of the entered numbers is: {average:.2f}")
if __name__ == "__main__":
main()
Comments
Post a Comment