"unsupported operand type(s) for -: 'int' and 'str'"


#1



I have allready a few times got this error: "unsupported operand type(s) for -: 'int' and 'str'",
can anyone please explain what this means?


#2

What exercise are you on?


#3

currently i'm on "Practice makes you perfect", exercise 7, but i've got this error already a couple more times in earlier exercices.


#4

The error you are receiving is because You are attempting to add together two different data types the python console gets confused when we try to do this. So to get around this you can concatenate your data types, to do this you will need to convert them both to strings or integers. :slight_smile:


#5

so this is my code now:

def reverse(text):
    r = ""
    for i in text:
        text = str(text)
        r = str(r)
        r += text[len(text) - i - 1]
    return r

I still get the error message after converting my 2 variables to strings. Can you see the problem? the goal is to return text in reverse ("abcd" should become "dcba").


#6

But now if you convert them to string types but on this line you are trying to use an integer it still will not work,


#7

Oke, but can you maybe say how I should do it then? I really don't know how else I can get the next last letter from the word...


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.