2016-05-01 125 views
1

我對編程非常陌生,python是我的第一個編程語言,所以請好。我已經運行下面的代碼位:TypeError:不支持的操作數類型爲 - :'str'和'int'Python

searchmovie = raw_input("What movie would you like to rent?\n").lower() 
searchindex = dvds.index(searchmovie) 
r = csv.reader(open('dvd_info.csv')) 
lines = [l for l in r] 
currentvalue = lines[searchindex][2] 
lines[searchindex][2] = currentvalue - 1 
writer = csv.writer(open('tmp.csv', 'w')) 
writer.writerows(lines) 

,我已經gotton錯誤:

lines[searchindex][2] = currentvalue - 1 
TypeError: unsupported operand type(s) for -: 'str' and 'int' 

我在做什麼錯?

+0

您的'currentvalue'是一個字符串,您需要先將其轉換爲'int'。 – AKS

+0

行'[searchindex] [2] = str(int(currentvalue) - 1)' – ozgur

+0

非常感謝!阿哈對不起,這是一個非常業餘的問題。 – Rab2233

回答

1

currentvalue是一個字符串,你需要將其轉換爲int第一:

lines[searchindex][2] = str(int(currentvalue) - 1) 

此代碼是從@Özgür的的評論中提取。

相關問題