# 计算n个数的最大值、最小值、平均值

2023-10-31 14:09:20

## 4 回答

TA贡献1817条经验 获得超6个赞

def avg_pos(d):

if len(d) == 0: # avoid div by 0

return 0

return sum(d)/len(d)

data = []

while True:

try:

n = input("Number: ")

if n == "#":

break

n = int(n)

data.append(n)

except ValueError:

print("Not a number")

print( f"Min: {min(data)}  Max: {max(data)}  AvgP: {avg_pos([d for d in data if d>0])}" )

Number: 4

Number: 5

Number: 6

Number: -2

Number: -99

Number: 73

Number: #

Min: -99  Max: 73  AvgP: 22.0

TA贡献1850条经验 获得超11个赞

maximum = None

minimum = None

sum_of_positive = 0

count_of_positive = 0

num = None

while True:

inp = input("PLease enter a number: ")

if inp == "#" :

break

try:

num=float(inp)

except:

print ("Error with this input")

continue

if maximum is None:

maximum = num

minimum = num

if num>maximum:

maximum=num

if num<minimum:

minimum=num

if num > 0:

sum_of_positive = sum_of_positive + num

count_of_positive = count_of_positive + 1

if count_of_positive > 0:

average_of_positive = sum_of_positive / count_of_positive

else:

average_of_positive = 0

print ("The Maximum is ", maximum)

print ("The Minimum is ", minimum)

print ("The Average of Positive Numbers is ", average_of_positive)

TA贡献1794条经验 获得超8个赞

TA贡献1824条经验 获得超5个赞

• 4 回答
• 0 关注
• 102 浏览

0/150