【问题标题】:How to find the average and median from the user's input? [duplicate]如何从用户的输入中找到平均值和中位数? [复制]
【发布时间】:2021-12-19 21:22:19
【问题描述】:

我是 python 语言的新手,我无法在我的书中找到如何编写这些函数。他们大多用伪代码编写,并希望您只知道如何准确编码。在我花了几个小时搜索没有结果之前,我想寻求帮助。

这个程序应该获取用户的房价并找到中位数、平均值、最大值和最小值。

#This program asks for prices of houses from the user, then it takes the input
#and calculates the AVG, MAX, MIN, and MED.
print("  Real Estate Program ")
print()
print("**********************************************************************")

#Will get the user's input about the price houses in their area
prices = []
cost = 0
while cost != -99:
    cost = input("Enter cost of one home or -99 to quit: ")
    prices.append(cost)
    if cost == "-99":
        break
    else:
        continue


print("*********************************************************************")

#removes -99 from list
prices.pop(-1)
print("Prices of homes in your area:")
print(prices)
print()

#this find the maximum value of the houses
print ("The maximum sale price is:", max(prices))

#this find the minimum value of the houses
print ("The minimum sale price is:", min(prices))

【问题讨论】:

标签: python python-3.x list


【解决方案1】:

对于这两种情况,您都可以使用统计模块。 您可以使用项目列表从统计中调用均值和中值函数。 所以你可以添加

from statistics import mean, median

print("The average sale price is:", mean(prices))
print("The median sale price is:", median(prices))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2011-11-09
    • 2016-04-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多