-4

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))
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 2
    Every time you append something to `s`, you first set it to an empty list. It will never contain more than one element. Move the initialization outside of the loop. And that `continue` is pointless, it does exactly what the code would do anyway, as there is nothing further that would happen during the current iteration of the loop. – jasonharper Aug 06 '20 at 00:47
  • Actually creating the list `s` is not necessary as you will end up with `e` again after transforming it back to a string. Also I don't see why you use enumerate instead of directly iterating through the string. – SimonT Aug 06 '20 at 01:00
  • Add some simple debugging print statements -- you will soon see that it's your code that doesn't work, not the `continue` statement. – augurar Aug 06 '20 at 01:03
  • And this is why you don't only look at test cases where the first digit is the same as the last digit... – superb rain Aug 06 '20 at 01:05
  • Thanks guys but the reason why I had to put the conversion of the list 's' to a string in the function because i need to feed the output into a return statement. Also I tried using the error handling statements( try, except and finally) but wasn't behaving as expected. Hopefully with your combined examples, I will figure it out. – Kwadwo Britwum Aug 12 '20 at 01:39

4 Answers4

2

Because you make the list in the loop. Just make it outside the loop. Also instead of str(s) which makes a string representation of the list use str.join as it will join all the elements of the list into a string. Also there is no need for the continue statement. As the elif won't run if the if is True.

for i, o in enumerate(e):
    s = []
    if o in "0123456789":
        s.append(o)
    else:
        print(type(e))
        return e
k = ''.join(s)
p = int(y)
return p
Jab
  • 26,853
  • 21
  • 75
  • 114
0

You have return in there, so the first time you hit a non-numeric character you're going to return that character and exit. As written, the continue won't do anything since the following elif won't be hit by any character that sends you down the first branch of the if statement.

theodox
  • 12,028
  • 3
  • 23
  • 36
0

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
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

If you regard only the integers of a string as its integer version, you can use isnumeric() check:

def resolve(e)
    return int(''.join([i for i in e if i.isnumeric()]))
Hamza
  • 5,373
  • 3
  • 28
  • 43