我编写了一个脚本来计算变量program_width
的值,但是我有一个奇怪的错误。
错误是:TypeError:+:'int'和'str'
不支持的操作数类型错误是跳到这一行:
program_start = 350 + program_width
以下是我使用的代码:
if datetime.timedelta(minutes = 10) <= program_duration <= datetime.timedelta(minutes = 30):
program_width = "250"
elif datetime.timedelta(hours = 1) <= program_duration <= datetime.timedelta(hours = 1.29):
program_width = "500"
elif datetime.timedelta(hours = 1.30) <= program_duration <= datetime.timedelta(hours = 1.45):
program_width = "750"
elif datetime.timedelta(hours = 1.46) <= program_duration <= datetime.timedelta(hours = 2):
program_width = "1000"
if program_width > 1:
if program_notification:
button_nofocus = 'channels_bar1.png'
button_focus = 'channels_yellow.png'
else:
button_nofocus = 'channels_bar1.png'
button_focus = 'channels_yellow.png'
if program_width < 65:
program_title = ''
else:
program_teststart = 350 + program_width
print program_teststart = 350 + program_width
我希望使用变量program_width
添加值来获取返回值,例如:350 + 500 = 850
。
有谁知道如何修复错误?
答案 0 :(得分:1)
在您指定program_width
的所有部分中尝试此操作:
program_width = 250
请注意,我删除了号码周围的""
。这些引号意味着该值是字符串,并且您无法向字符串添加数字,我很确定您打算将program_width
用作整数。现在这将有效:
program_teststart = 350 + program_width
答案 1 :(得分:1)
您将program_width
设置为字符串 "500"
,而不是 int 500
。然后,您尝试将其添加到 int 350
。这些是不同的类型,错误告诉你究竟是什么错,你不能添加整数和字符串。
我的猜测是你希望它从一开始就是一个int,所以
program_width = "500"
应该是
program_width = 500
但是如果你真的希望它出于某种原因而成为一个字符串(你几乎肯定不会),你可以使用int()
函数将字符串转换为int。
program_start = 350 + int(program_width)