How to accept 0 as a value in a list? and When I enter the numbers after a space I get an error
<p>Everytime I enter a value such as 002
the length comes out as 1 however it should come out as 3
My code is:</p>

<pre><code>list1  = int(input("Enter a number"))
list1 = [int(x) for x in str(list1)]
print (len(list1))
</code></pre>

<p>If I type in a number after a space I get an error saying:</p>

<pre><code>File "C:\\practice11.py", line 2, in &lt;module&gt;
  list1 = [int(x) for x in text]
File "C:\\practice11.py", line 2, in &lt;listcomp&gt;
  list1 = [int(x) for x in text]
</code></pre>

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

<p>Because you cast the user input <code>"002"</code> to an int with value <code>2</code>. Then, in your list comprehension you cast it back to a string <code>"2"</code>, over which you iterate (once). </p>

<p>The result of your list comprehension is thus <code>["2"]</code>, a list of length 1.</p>

<p>So instead of casting back and forth you could:</p>

<pre><code>list1 = [int(x) for x in input("Enter a number")]
</code></pre>

<p>Which will give you <code>list1 = [0, 0, 2]</code>, a list of integers with a total length of 3. </p>

python