gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for -: 'str' and 'int' Python

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:18 25 4
gpt4 key购买 nike

我对编程很陌生,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)

我得到了错误:

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

我做错了什么?

最佳答案

您的currentvalue是一个字符串,您需要先将其转换为int:

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

此代码摘自@ozgur 的评论。

关于python - 类型错误 : unsupported operand type(s) for -: 'str' and 'int' Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36964258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com