# Python
arr = []
while True:
try:
n = int(input("Number: "))
if n == 0:
break
arr.append(n)
except:
print("An integer must be entered!")
neg_num = [x for x in arr if x < 0] # Negative numbers
ar_mean = round(sum(neg_num)/len(neg_num), 4)
except ZeroDivisionError:
print("There is no negative numbers" )
ar_mean = '-'
pos_num = [x for x in arr if x > 0] # Positive numbers
product = 1
for number in pos_num:
product *= number
geo_mean = round(product ** (1/len(pos_num)), 4)
print("There is no positive numbers" )
geo_mean = '-'
print("Arithmetic mean of negative numbers is equal to", ar_mean)
print("Geometric mean of positive numbers is equal to", geo_mean)
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
# Python
arr = []
while True:
try:
n = int(input("Number: "))
if n == 0:
break
arr.append(n)
except:
print("An integer must be entered!")
neg_num = [x for x in arr if x < 0] # Negative numbers
try:
ar_mean = round(sum(neg_num)/len(neg_num), 4)
except ZeroDivisionError:
print("There is no negative numbers" )
ar_mean = '-'
pos_num = [x for x in arr if x > 0] # Positive numbers
product = 1
for number in pos_num:
product *= number
try:
geo_mean = round(product ** (1/len(pos_num)), 4)
except ZeroDivisionError:
print("There is no positive numbers" )
geo_mean = '-'
print("Arithmetic mean of negative numbers is equal to", ar_mean)
print("Geometric mean of positive numbers is equal to", geo_mean)