gpt4 book ai didi

python - 我的 min(list) 函数产生错误的输出,我不知道为什么

转载 作者:行者123 更新时间:2023-12-01 08:10:10 25 4
gpt4 key购买 nike

我正在运行一个非常基本的程序来查找我生成的列表的最小/最大值,并且我得到了错误的输出,但不知道为什么。我附上显示输出的屏幕截图。尽管该列表由于其长度而未完全显示,但已显示的内容足以表明我的 min 和 max 函数是错误的。代码也包含在内。 Output of my min/max functions

print("Welcome to EPA Mileage Calculator")
yearChoice = int(input("What year would you like to view data for? (2008 or 2009): "))
while yearChoice != 2008 and yearChoice != 2009:
print("Invalid input, please try again")
yearChoice = int(input("What year would you like to view data for? (2008 or 2009): "))
saveResults = input("Enter the filename to to save results to: ")

if yearChoice == 2008:
fileIn = open("epaVehicleData2008.csv", "r")
fileIn.readline()
minMPG = []
maxMPG = []
for line in fileIn:
line = line.strip()
dataList = line.split(",")
if dataList[0] not in ['VANS - PASSENGER TYPE', 'VANS - CARGO TYPE', 'TRUCK', 'MINIVAN - 4WD', 'MINIVAN - 2WD']:
minMPG.append(dataList[8])
#print(minMPG)
maxMPG.append(dataList[8])
#print(maxMPG)
print(minMPG)
print(min(minMPG))
print(maxMPG)
print(max(maxMPG))

最佳答案

这是因为您使用的是字符串。

首先,将字符串改为整数。 使用:minMPG.append(int(dataList[8]))

maxMPG.append(int(dataList[8]))相同

这应该有效。

关于python - 我的 min(list) 函数产生错误的输出,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55310100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com