Why do I get ValueError in one piece of code but not the other although they are identical?
<p>I've been solving Project Euler problems and here is my code for <a href="https://projecteuler.net/problem=35" rel="nofollow">problem 35</a>:</p>

<pre><code>def sieve_of_Erathosthenes(): 
    sieve = [True] * 10**6
    def mark(sieve, x):
        for i in xrange(x+x, len(sieve), x):        
            sieve[i] = False
    for x in xrange(2, int(len(sieve) ** 0.5) + 1):         
        if sieve[x]:
        mark(sieve, x)
    return list(str(i) for i in range(2, len(sieve)) if sieve[i])


def is_prime(n): 
    from math import sqrt
    if all(n % i != 0 for i in xrange(2,int(sqrt(n))+1)):
        return True

def is_circular_prime(p): 
    if all(is_prime(int(p[i:] + p[:i])) for i in xrange(len(p))):
        return True

primes = sieve_of_Erathosthenes()
number_of_circular_primes = 0

for i in primes: 
    if is_circular_prime(i):
        number_of_circular_primes += 1

print number_of_circular_primes
</code></pre>

<p>It works just as I intended and outputs the correct solution. I used a similar approach to solve <a href="https://projecteuler.net/problem=37" rel="nofollow">problem 37</a> but I get: " ValueError: invalid literal for int() with base 10: ''. " I tried everything and it still didin't work. Here is my code:</p>

<pre><code>def sieve_of_Erathosthenes(): 
    sieve = [True] * 10**5
    def mark(sieve, x):
        for i in xrange(x+x, len(sieve), x):        
            sieve[i] = False
    for x in xrange(2, int(len(sieve) ** 0.5) + 1):         
        if sieve[x]:
        mark(sieve, x)
    return [str(i) for i in range(2, len(sieve)) if sieve[i] and i &gt; 10]

def is_prime(n): 
    from math import sqrt
    if all(n % i != 0 for i in xrange(2,int(sqrt(n))+1)):
        return True

def is_trunctable_from_the_right(n):
    if all(is_prime(int(n[:i])) for i in range(len(n)-1)):
        return True

def is_trunctable_from_the_left(n):
    if all(is_prime(int(n[i:])) for i in range(1,len(n))):
        return True


primes = sieve_of_Erathosthenes()
trunctable_from_both_sides = 0

for i in primes:
    if is_trunctable_from_the_left(str(i)) and is_trunctable_from_the_right(str(i)):
        trunctable_from_both_sides += int(i)

print trunctable_from_both_sides
</code></pre>

<p>As you can see most of the code here is just copied and pasted from the previous problem. Why doesn't it work this time? How do I fix this?</p>

<p>You don't mention the line on which you get the error. That would have helped. Also you know much of the code works. So just focus on the bit which is different: the truncations. </p>

<p>I suspect the error is in this <code>int(n[:i])</code></p>

<p>The range starts from 0, so the first term is a zero length string</p>

<pre><code>ValueError: invalid literal for int() with base 10: ''
</code></pre>

<p>The invalid literal string is the empty string, It can't convert that to a number. So fix the range statement:</p>

<pre><code>(isprime(int(n[:i])) for i in range(1,len(n)))
</code></pre>

python