Home > Mobile >  Error "object 'int' not iterable" in custom "seq" function
Error "object 'int' not iterable" in custom "seq" function

Time:11-25

I'm trying to create a seq function which would create a list from from to to, but I get the error "object 'int' not iterable". What's wrong?

def seq (ffrom, to, by=1):
    x = ffrom
    l = list(x)
    #extending list
    while x < to:
        l.append(x 1)
        x = x   1
    #Returning value
    tuple(l)
    print(l)

I get the error that the the list xline isn't iterable.

CodePudding user response:

The notation list(x) tries to build a list with all elements of x, but as the error states : int isn't iterable

What you want is

l = [x]

def seq(ffrom, to, by=1):
    x = ffrom
    l = [x]
    while x < to:
        x  = by
        l.append(x)
    print(l)


seq(5, 8)
print(list(range(5, 9)))

CodePudding user response:

l = list(x) needs to be l = [x] to make a list of one item with x in it:

def seq(ffrom, to, by=1):
    x = ffrom
    l = [ffrom]

    #extending list
    while x < to:
        x = x   1
        l.append(x)
    
    return l
  • Related