9
Hi, I’m creating a character converter for your ascii code through a recursive Python function. However I have a problem "'int' Object is not iterable".
This is my code:
def cod(n):
for i in n:
i = str(i)
if len(n) == 1:
ascii = ord(i)
return ascii
else:
ascii = ord(i) + cod(ord(i))
return ascii
the error that returns is as follows:
Traceback (most recent call last):
File "python", line 1, in <module>
File "python", line 8, in codifica
File "python", line 2, in codifica
TypeError: 'int' object is not iterable
I’m basically resorting to ord()
to get the character number in ASCII. When I call just one character everything goes well. If you try to pass more than one problems start.
The problem apparently is the variable i
being treated as an integer but supposedly I convert into a string when doing i = str(i)
What I want is that when performing the function cod('ola')
she returns 11110897
which corresponds to the conversion of each of the characters to ASCII.
Can someone help me figure out where I’m missing?
Just one question: is the return of this function supposed to be a string or a number? I answered assuming it is a string (otherwise, a call
cod("anticonstitucionalissimamente")
was going to give overflow...), but if it is something different please specify. Including in your lineascii = ord(i) + cod(ord(i))
you’re adding up numbers, which would make111 + 108 = 219
and not111108
.– mgibsonbr
That sum really didn’t make any sense... but since I hadn’t even been able to convert two characters yet, I didn’t notice. Rookie mistake.
– Marcos Lopes