What are the errors inside this random walking code? : <p>I am having unexpected outputs with the following code:</p>

<pre><code>import random

N = 30  # number of steps

n = random.random()  # generate a random number

x = 0
y = 0
z = 0
count = 0 
while count &lt;= N:

if n &lt; 1/3:
    x = x + 1           # move east
    n = random.random() # generate a new random number

if n &gt;= 1/3 and n &lt; 2/3:
    y = y + 1           # move north
    n = random.random() # generate a new random number

if n &gt;= 2/3:
    z = z + 1           # move up
    n = random.random() # generate a new random number


print("(%d,%d,%d)" % (x,y,z))
count = count + 1
</code></pre>

<p>When I run the code, the problem is:</p>

<ul>
<li>Code output displays 31 coordinates, 1 more than the number of steps (N) variable.</li>
<li>Each iteration for 1 step should take only 1 step but it sometimes take multiple steps.</li>
</ul>

<p>When I tested the code, the problem is ensured. To test the code, I assigned N = 1, and saw the following output:</p>

<ul>
<li><p>(-1,0,1) This should be the initial step, but it took multiple steps (both x-1 and z+1), how could this happen?</p></li>
<li><p>(-2,0,1) Number of step variable (N) = 1 but this is the second output, why was it displayed? 
Thanks for helping</p></li>
</ul>
