python - 总和、平均值及其他

标签 python

我正在尝试将列表中的整数转换为列表的总和和平均值,并说明低于冰点 F<32 的任何温度。 每当我尝试获取总和或平均值时,我都会收到错误消息“+ 不支持的操作数类型:‘int’和‘str’”。我还知道 (sum(list) 目前位于错误的位置。

#Input
count = 0
list = []

while True:
    Temp = input("Enter a Temperature, enter q when done ")
    if Temp.lower() == 'q': 
      break
    list.append(Temp)
    count = count + 1
    print("List = ",list)
print("Number of Temperatures = ", count)
print("Highest Temperature = ",max(list))
print("Lowest Temperature = ",min(list))
print("Average Temperature= ",sum(list))

最佳答案

尝试使用以下代码:

l= []

while True:
    Temp = input("Enter a Temperature, enter q when done ")
    if Temp.lower() == 'q': 
      break
    Temp = int(Temp)
    l.append(Temp)
    print("List = ",l)
print("Number of Temperatures = ", len(l))
print("Highest Temperature = ",max(l))
print("Lowest Temperature = ",min(l))
print("Average Temperature= ",sum(l)/len(l))

有两个变化:

  1. 您必须在某个时候将它们转换为integer。

  2. sum 给出的是 sum,而不是平均值,因此您需要将 sum 除以 length。

  3. 删除了您的计数,因为不需要。

  4. list 更改为 l,因为它是默认关键字。

关于python - 总和、平均值及其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56658890/

相关文章:

python : read text file character by character in loop

python - 将两个 DataFrame 合并为 block

python - Django 查询集

python - 在 fractions.Fraction 子类中调用 super().__init__(*args, **kwargs) 只接受要初始化的实例

Python:运行一个进程/线程,即使在主进程完成后仍继续运行

python - 将多个 CSV 文件中的列数据合并到单个 CSV 文件中

python - 使用不同长度的 bool 系列从数据框中选择行

python - 在对抗训练期间是否应该重复使用 Dropout 面具?

python - 如何抓取ajax返回的网页内容?

python - 查找二维列表中特定列的长度