I have the following little function written in Python:
def encode(str):
out = ""
for i in str:
ret += str(ord(i.upper()) - 64)
return ret
Basically, what I want to do is get the number of the letter in the alphabat and concatenate it to the ‘out’ string. With this code I get a traceback at line 4: ‘str’ object is not applicable.
Could someone please explain me why it throws this error and how I can fix this? (Sorry if this was already asked once, I couldn’t find it, probably also because I’m pretty new to Python and programming)
Never name your variable on the pre-defined built-in name.
In your code,
stris not a built-in function. It’s the variable you have used as parameter in your function.Another problem is, you have declared
outvariable, and usingretwhich will give you error. Changeout = ""toret = "".