Home > Back-end >  Getting error as: 'int' object is not iterable
Getting error as: 'int' object is not iterable

Time:02-15

if __name__ == '__main__':
    i = 0
    j = 0
    flag = 0
    S = 2
    E = 10
    for i in range(S, E   1):
        if (i == 1):
            continue
        flag = 1
        for j in range(2, i // 2   1):
            if (i % j == 0):
                flag = 0
                break
        if (flag == 1):
            print(list(i))

I need to find the length of the total integers between an interval and for this code. I am getting an error

CodePudding user response:

Well error is comming becose you are trying to list through an intiger. In for loop i is an intiger(int).

CodePudding user response:

You are trying to convert an int object to a list object.

If you want to create a list with every value of i in every iteration, you can gradually add the i value to a list.

if __name__ == '__main__':
    example = []
    i = 0
    j = 0
    flag = 0
    S = 2
    E = 10
    for i in range(S, E   1):
        if (i == 1):
            continue
        flag = 1
        for j in range(2, i // 2   1):
            if (i % j == 0):
                flag = 0
                break
        if (flag == 1):
            example.append(i)
    print(example)

CodePudding user response:

The error is with this line

print(list(i))

Change this line to:-

print(list(str(i)))

You were getting error because python cannot list an integer(int) , you to change it to string using str().

if you got the answer, then please accept it and upvote it.

Thank You

  • Related