Search code examples
pythoncontinue

Continue doesn't seem to work in this simple Python function


The function is supposed to take a string as its input and return (if all members of the string are digits) and integer version of that string. The string provided in this example is a 3 digit number. the function's for loop seems to only return the first digit only hence the continue may not be working as expected.

e = '484'

def resolving(e):
    for i, o in enumerate(e):

        if o in "0123456789":

            s = []
            s.append(o)
            i += 1

            continue
                
        elif o not in "0123456789":
            print(type(e))
            return e

    k = str(s)
    y = k.replace("'","").replace("[","").replace("]","").replace(",","").replace(" ","")
    p = int(y)
    print(type(p))
    return p

print(resolving(e))

Solution

  • At the risk of completely missing the point of what you're trying to do, this entire function could just be:

    def resolve(e):
        """If e is convertible to an int, return its int value; 
        otherwise print its type and return the original value."""
        try:
            return int(e)
        except ValueError:
            print(type(e))
            return e