Python - 'int' Object is not iterable

Asked

Viewed 13,617 times

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?

  • 1

    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 line ascii = ord(i) + cod(ord(i)) you’re adding up numbers, which would make 111 + 108 = 219 and not 111108.

  • 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.

2 answers

7


Your problem is here:

cod(ord(i))

You are calling the function cod recursively passing the result of ord - and therefore a whole - as argument. But as the first thing that cod makes is iterate on the argument:

for i in n: # n na segunda chamada é o resultado de ord(i)

So he complains that he can’t iterate over an integer.

How are you wearing one for, I’m guessing your intention wasn’t to create recursive code. In this case, create a string "result" and go throwing the values found in it, to in the end return it (this even avoids this if where you test if the string size is 1 or not):

def cod(n):
    resultado = ""
    for i in n:
        # i já é uma string, não precisa fazer i = str(i)
        ascii = ord(i)
        resultado += str(ascii) # ascii é um número, então precisa fazer str(ascii)
    return resultado

A recursive solution, on the other hand, would not need a for. Just check whether the string is empty or not, and if it is not, apply the ord to its first character and concatenate with the result of the application of cod to its suffix:

def cod(n):
    return "" if n == "" else str(ord(n[0])) + cod(n[1:])
  • That’s right! : ) I wasn’t going to get there, I tried all sorts of things, including iterating over lists but then 'Ord()' didn’t work. Thank you very much!

  • By the way, is there any way to do this using a recursive function?

  • 1

    @Marcoslopes Yes, see the edited answer.

0

I am assuming that the error that is giving is in the first loop of the FOR, where it does not execute does not iterate the variable i until reaching the n.

The best way to solve this is by converting the condition to range and would stay that way:

def cod(n):
for i in range(len(n)):
    i = str(i)    
    if len(n) == 1:
        ascii = ord(i)
        return ascii
    else:
        ascii = ord(i) + cod(ord(i))
        return ascii

If you want to use n as an integer in the future, use

for i in range (1, n) :

Browser other questions tagged

You are not signed in. Login or sign up in order to post.