0
votes

I've written a script to work out the value with variable program_width, but I have got a weird error.

The error is: TypeError: unsupported operand type(s) for +: 'int' and 'str'

The error are jumping on this line:

program_start = 350 + program_width

Here is the code I use:

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

I'm expecting to add the value with the variable program_width to get the return value, example: 350 + 500 = 850.

Do anyone know how to fix the error?

2

2 Answers

1
votes

You're setting program_width to the string "500" instead of the int 500. Then you're trying to add it to the int 350. These are different types, and the error is telling you exactly what's wrong, you can't add integers and strings.

My guess is that you want it to be an int from the beginning, so

program_width = "500"

should be

program_width = 500

But if you really want it to be a string for some reason (you almost certainly don't), you can cast the string into an int with the int() function.

program_start = 350 + int(program_width)
1
votes

Try this, in all the parts where you're assigning program_width:

program_width = 250

Notice that I removed the "" surrounding the number. Those quotes mean that the value is a string, and you can not add a number to a string, I'm pretty sure that you intended to use program_width as an integer. Now this will work:

program_teststart = 350 + program_width