Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的min(list)函数产生了错误的输出,我不知道为什么_Python - Fatal编程技术网

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

Python 我的min(list)函数产生了错误的输出,我不知道为什么,python,Python,我正在运行一个非常基本的程序来查找我生成的列表的最小/最大值,我得到了错误的输出,但不知道为什么。我附上显示输出的屏幕截图。虽然列表由于其长度而未完全显示,但显示的内容足以表明我的最小值和最大值函数是错误的。代码也包括在内 这是因为您正在使用字符串 首先,将字符串更改为整数。 用法:minMPG.append(int(dataList[8])) 同样适用于maxMPG.append(int(dataList[8])) 这应该行得通。没那么简单行不包含仅可转换为int的值。是。因此,它应该将dat

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


这是因为您正在使用字符串

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

同样适用于
maxMPG.append(int(dataList[8]))


这应该行得通。

没那么简单<代码>行不包含仅可转换为
int
的值。是。因此,它应该将dataList[8]转换为int。希望现在它是正确的!请以文本形式提供代码的输出。
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))