简体   繁体   中英

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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM