不支持的操作数类型 - :'int'和'str'

时间:2016-03-14 22:21:54

标签: python

我报告了以下错误:

unsupported operand type(s) for -: 'int' and 'str'

使用此代码:

def main():  

# step 1
print "LOAD CASE BASE"  
# using csv
# into array of classes
# filepath = 'C:\\Users\\standalone\\Dropbox\\PYTHONDEV\\Scripts\\BCO6004\\CASEBASE.txt'
filepath = 'C:\Users\Trip Admin\Documents\Python Scripts\IntelSysAssignment\CASEBASE.txt'
casebase = readCSVintoList(filepath)

for case in casebase:
    print case
    print case[1]
    c1 = Camera(case[0], case[1], case[2], case[3]) # overwritten, so store in an array of cases
    print c1.get_special_features(), c1.get_optical_sensor_resolutions(), c1.get_optical_sensor_size(), c1.get_optical_zoom()


# step 2
print "GET USER INPUT" 
#2special_features =              input("Enter the camera's special_features: ")
optical_zoom =                  int(input("Enter the camera's optical_zoom: "))
optical_sensor_size =           int(input("Enter the camera's optical_sensor_size: "))
optical_sensor_resolutions =    int(input("Enter the camera's optical_sensor_resolutions: "))
unknowncase = Camera("special_features", optical_zoom, optical_sensor_size, optical_sensor_resolutions)  


# step 3
print "CALCULATE SIMILARITY"
similarity = unknowncase.similarity(c1)


 # step 4
print "PRINT OUT RESULT"   
print similarity

class Camera(object):


def __init__(self, special_features="", optical_zoom=0, optical_sensor_size=0, optical_sensor_resolution=0):
    self.special_features = special_features                    # e.g. Serial Shot Mode
    self.optical_zoom = optical_zoom                            # e.g. 5x, or 8x
    self.optical_sensor_size = optical_sensor_size              # e.g. 1/2.3
    self.optical_sensor_resolution = optical_sensor_resolution  # e.g. 16.1MP, or 20.1MP 


def get_special_features(self):
    return self.special_features

def get_optical_zoom(self):
    return self.optical_zoom

def get_optical_sensor_size(self):
    return self.optical_sensor_size

def get_optical_sensor_resolutions(self):
    return self.optical_sensor_resolution  


def similarity(self, camera):
    sim = (self.optical_zoom - camera.optical_zoom)/10                                  # between 0 and 10
    sim = sim + (self.optical_sensor_size - camera.optical_sensor_size)/3               # between 0 and 3
    sim = sim + (self.optical_sensor_resolution - camera.optical_sensor_resolution/25)  # between 0 and 25
    return sim/3

我已经检查了其他帖子,并在这里添加了int输入,如上所示,但我仍然收到错误。

1 个答案:

答案 0 :(得分:1)

int()演员必须不在这里

c1 = Camera(case[0], case[1], case[2], case[3]) 

例如,

c1 = Camera(int(case[0]), int(case[1]), int(case[2]), int(case[3]))

或者

c1 = Camera(*[int(x) for x in case[0:4])

进一步观察,我想它必须是float(...)而不是int(...)