Roman Davis
Roman Davis

Reputation: 351

typeerror unsupported operand type(s) for %: 'list' and 'int'

Here's the code:

list = [2, 3, 5, 7, 11, 13]
list2 = [range(list[-1], 2000000)]
y =11
x = 1
v = list[-1]>= x
while list[-1] ** 2 < 2000000:
    y= y + 2
    prime = True
    while prime == True:    
        for x in list:
            if x * 2 < y:   
                if y % x == 0:
                    prime = False
                    break
        if prime == True:
            list.append(y)
            prime = False
print sum(list)

for u in list:
    for w in list2:
        if u * u < w:
            if w % u == 0:
                list2.pop(w)
print list
print sum(list) + sum(list2)

As you see, it's a basic program that creates a sieve and then puts numbers up to two million for it. It's for project Euler and I'm trying to test my skills as I learn to program.

Right now, the error, for which this post is titled is on line 23. Any reason why this is happening?

Upvotes: 3

Views: 7893

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124928

range() already returns a list, but you put it into a new list:

list2 = [range(list[-1], 2000000)]

This results in a list containing a list, and w later on is set to the full range. Just remove the brackets there.

>>> [range(5)]
[[0, 1, 2, 3, 4]]
>>> range(5)
[0, 1, 2, 3, 4]

Upvotes: 8

Related Questions