TypeError:不支持的操作数类型为 - :'str'和'int'Python
问题描述:
我对编程非常陌生,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'
我在做什么错?
答
你currentvalue
是一个字符串,你需要将其转换为int
第一:
lines[searchindex][2] = str(int(currentvalue) - 1)
此代码是从@Özgür的的评论中提取。
您的'currentvalue'是一个字符串,您需要先将其转换为'int'。 – AKS
行'[searchindex] [2] = str(int(currentvalue) - 1)' – ozgur
非常感谢!阿哈对不起,这是一个非常业余的问题。 – Rab2233