When I run my code it tells me: Type Error: unorderable types: str() < float(). I can't figure out why it won't let me compare these two numbers. The list I am using is defined, and the numbers in it have been redefined as floats, so I'm not sure what else to do. Any suggestions?
def countGasGuzzlers(list1, list2):
total = 0
CCount = 0
HCount = 0
for line in list1:
if num < 22.0:
total = total + 1
CCount = CCount + 1
for line in list2:
if num < 27.0:
total = total + 1
Hcount = Hcount = 1
print('City Gas Guzzlers: ',CCount)
print('Highway Gas Guzzlers: ',HCount)
print('Total Gas Guzzlers: ',total)
This is my list definition. I'm pretty sure it's fine, but maybe there are some bugs in here as well?
CityFile = open('F://SSC/Spring 2015/CSC 110/PythonCode/Chapter 8/HW 4/carModelData_city','r')
for line in CityFile:
CityData = CityFile.readlines()
for num in CityData:
numCityData = float(num)
CityList = numCityData
HwyFile = open('F://SSC/Spring 2015/CSC 110/PythonCode/Chapter 8/HW 4/carModelData_hwy','r')
for line in HwyFile:
HwyData = HwyFile.readlines()
for num in HwyData:
numHwyData = float(num)
HwyList = numHwyData
I believe you are incorrectly referencing to
numinstead oflinewhich is the counter variable in yourforloops, you either need to usenumas the counter variable, or uselinein theifcondition.Another issue I see is with the way you are creating the lists . The issue is that you are converting each num in file to float and then storing that directly in list variable, which causes list variable to actually store a float value instead of a list, you need to append each value into list, instead of doing
list = numThe code would look like -
Please also make sure CityList and HwyList are initialized before this code as lists. Like below -