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>If I run your code, you'll notice the error happens when <code>i</code> is 11:</p>

<pre><code>&gt;&gt;&gt; primes = sieve_of_Erathosthenes()
&gt;&gt;&gt; 
&gt;&gt;&gt; trunctable_from_both_sides = 0
&gt;&gt;&gt; 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)
... 
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 2, in &lt;module&gt;
  File "&lt;stdin&gt;", line 2, in is_trunctable_from_the_right
  File "&lt;stdin&gt;", line 2, in &lt;genexpr&gt;
ValueError: invalid literal for int() with base 10: ''
&gt;&gt;&gt; i
'11'
&gt;&gt;&gt; 
</code></pre>

<p>So, digging into the Traceback, we see you end up with a situation like this in <code>is_trunctable_from_the_right</code>:</p>

<pre><code>&gt;&gt;&gt; for x in range(len(n)-1):
...   print n[:i]
... 

&gt;&gt;&gt; n
'11'
&gt;&gt;&gt; range(len(n)-1)
[0]
</code></pre>

<p>So you are returning an empty string when you truncate your string, passing that empty string to <code>int</code>, which is throwing the error.</p>

python