从if语句Python 3中获取平均值(Obtaining an average from if statement Python 3)

编程入门 行业动态 更新时间:2024-10-19 05:28:05
从if语句Python 3中获取平均值(Obtaining an average from if statement Python 3)

好吧,我把所有的东西都弄清楚了。 但是现在我正在尝试使用if语句来告诉循环从低于22 mpg的车辆获取平均mpg(来自.txt文件),然后将其平均然后输出IDLE。 我觉得我应该在循环之前运行if语句。 我只是不确定我真的需要改变。 我想我应该能够切换保存文本文件数据的变量。 反正这里是我的代码。 有人能帮助我理解我做得对吗?

def cityGasGuzzler(): # Input:that assigns a text file to a value and provides other definition values cityGuzz = open("carModelData_city.","r") # Process: For loop to get average of gas guzzling city street driving vehicles for line in cityGuzz: # Process: Uses if statement to get the average of lower mpg or gas guzzlers if cityGuzz[0:-1] < 22: sum = sum + eval(line) count = count + 1 avrg = sum / count # Output: using the round function to get average to the 2nd decimal place # and prints string and rounded variable to IDLE. print("The average city MPG is,", round(avrg, 2)) cityGasGuzzler()

只是为了澄清,我的主要目标如下,从文本文件中的数值小于22,平均起来,并输出平均值为空闲。

Ok so I got everything figured out. But now I am trying to use an if statement to tell the loop to take the average mpg (from the .txt file) from vehicles that get less than 22 mpg and then just average that up and output it IDLE. I feel like I should run the if statement before the loop. I am just not sure what I really need to change. I figure I should be able to slice variable holding the text file data. anyways here is my code. Can someone help me understand what I am not doing right?

def cityGasGuzzler(): # Input:that assigns a text file to a value and provides other definition values cityGuzz = open("carModelData_city.","r") # Process: For loop to get average of gas guzzling city street driving vehicles for line in cityGuzz: # Process: Uses if statement to get the average of lower mpg or gas guzzlers if cityGuzz[0:-1] < 22: sum = sum + eval(line) count = count + 1 avrg = sum / count # Output: using the round function to get average to the 2nd decimal place # and prints string and rounded variable to IDLE. print("The average city MPG is,", round(avrg, 2)) cityGasGuzzler()

Just to clarify, my main objective is as follows, take the numeric values from the text file that are less than 22, average those up and output the average to IDLE.

最满意答案

如果我正确理解了这个问题,那么您的问题就出在这一行:

if cityGuzz[0:-1] < 22:

由于几个原因,这cityGuzz是因为cityGuzz是文件对象,而不是当前行。 您需要将当前行转换为数字,然后将其与数字22进行比较。

尝试这样的事情:

total = 0 # renamed to avoid masking the builtin sum function count = 0 for line in cityGuzz: mpg = float(line) if mpg < 22: total += mpg count += 1 average = total / count

我将sum重新命名为total因为sum是内建函数的名称。 事实上,它可能是一个有用的替代显式循环来合并文件中的值。 这是平均计算的替代实现:

guzzlers = [mpg for mpg in map(float, cityGuzz) if mpg < 22] average = sum(guzzlers) / len(guzzlers)

If I understand the question correctly, your issue is with this line:

if cityGuzz[0:-1] < 22:

That's not working for several reasons, not least because cityGuzz is the file object, not the current line. You need to convert the current line into a number before comparing it with a number like 22.

Try something like this:

total = 0 # renamed to avoid masking the builtin sum function count = 0 for line in cityGuzz: mpg = float(line) if mpg < 22: total += mpg count += 1 average = total / count

I renamed sum to total because sum is the name of a builtin function. In fact, it might be a useful one to replace the explicit loop to add up the values from the file. Here's an alternative implementation of the average calculation:

guzzlers = [mpg for mpg in map(float, cityGuzz) if mpg < 22] average = sum(guzzlers) / len(guzzlers)

更多推荐

本文发布于:2023-08-04 09:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   语句   Python   statement   Obtaining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!