Unsupported operand type - str, int

In this practice problem for strings, I have to insert the word “greg” into the middle of the given string. The code is returning correctly, but the website is giving me the error:

  unsupported operand type(s) for -: ‘str’ and ‘int’
def gregs_e_tagger(string):
  if len(string) % 2 == 0:
    middle_index = int(len(string) / 2)
    middle_index = int((len(string - 1)) / 2)
  greg = string[:middle_index] + 'greg' + string[middle_index:]
  return greg

I actually had different code at first which also worked.

def gregs_e_tagger(string):
  if len(string) % 2 == 0:
    middle_index = len(string) // 2
    middle_index = (len(string - 1)) // 2
  greg = string[:middle_index] + 'greg' + string[middle_index:]
  return greg

What is this error I’m getting? The code is running fine, so I’m not sure if there’s really a problem.
Thanks for your input!

Not sure how you’re measuring that to be working, it probably isn’t.

If you read your error message, do you agree that the thing described there indeed does not make sense? And, where in your code are you doing that? That error message is incomplete so if you look at the full version that’ll be pointing out the location too, though, it’s easy to locate once you’ve read it and have understood what the bad situation is.

You might not read the error message carefully enough even though it really does say everything you need, and therefore not understand it. But it is also googleable (like many error messages) which would get you a bunch of clarifications on what it is saying.

You also have the option of commenting things out if you still aren’t seeing the source of the problem. That lets you rule things out, so that you can then slowly add things back until you re-introduce the problem at which point you know which addition caused the problem since you ran it without crashing previously.

When I say it’s working I mean that when I click run, the correct result comes out. Like when I type print(gregs_e_tagger(“banana”)), it prints bangregana.

I’ve been getting this kind of error before when I divide a number. Because it’s returning a float, but to use the number as an index, it has to be an integer. So I searched and found that "hard floor"ing it or using // instead of just / will keep the divided number the same type it was before being divided.

So since it’s printing to the terminal, I’m not sure how to reconcile the error message.

remove/add a letter from your word

Ahh, I see! Thanks for the help!

And yeah you should not be doing floating point math just to convert back to int. Stick with integer math, float has nothing to do with it.

…additionally, dividing by two will discard the extra one, so those two cases are not different

You mean don’t do the hard floor thing? Good to know!

If you don’t then you have floats, you shouldn’t have floats when you’re doing integer math.

Okay… so every time I divide integers, do I have to make sure it stays an integer using int() ? Because just dividing it without seems to be giving me errors, which is how I found //

If it stays integer you wouldn’t need to convert to integer because it already is integer.

If you get a float the solution is not to “fix” it by converting it back. You should never have one in the first place.

So I should be able to use this?

def gregs_e_tagger(string):
  if len(string) % 2 == 0:
    middle_index = len(string) / 2
    middle_index = (len(string) - 1) / 2
  greg = string[:middle_index] + 'greg' + string[middle_index:]
  return greg

Without int() around the division it gives me this error:

Traceback (most recent call last):
… line 26, in
… line 24, in gregs_e_tagger
greg = string[:middle_index] + ‘greg’ + string[middle_index:]
TypeError: slice indices must be integers or None or have an index method

that’s got floats. you shouldn’t have those.

and, again, your if-statement is redundant since both your branches do exactly the same thing

Are the floats in the len(string) part? I don’t understand that.

you can test the type of a value, and no

you should use integer division

You have two integers. You want an integer result. What do? Floating point operation? No of course not.

Sorry, what’s a floating point operation? I’m pretty new obviously so I haven’t heard of a lot of this stuff. I don’t get the difference between dividing an integer and a float.

A type represents some particular kind of value and has associated operations.

You have integers, you want an integer. So you would use one of the operations that belong to the integer type. No conversions should be involved.

If you have a list and want to append to it, do you convert that to dict, make an insertion and back to list again?

How would I divide an integer if not with /?

Isn’t that what // does? Not sure why you’re ruling it out as an option.

So you’d remove the redundant if-statement, and then you’d have:

def gregs_e_tagger(s):
    half = len(s // 2)
    return s[:half] + "greg" + s[half:]

You should in general remove code that doesn’t do anything, that’s just making things difficult.

Oh… I thought you meant that was floating point math!