0

The task I am trying to address is probably best explained thus:

  1. Consider a cube. Each side (a,b,c) has a length of 1 unit.
  2. Place a dot at the point where all a,b and c intersect (position 0,0,0).
  3. Find the energy at that point.
  4. Move the point 0.1 units along side a (position 0.1,0,0).
  5. Repeat steps 3 and 4 until you reach the end of side a (i.e. points 0...1,0,0 have been considered).
  6. Return to the start of side a and take a step of 0.1 along side b (position 0,0.1,0)
  7. Repeat steps 3-6 until you reach the end of sides a and b (i.e. points 0...1,0...1,0 have been considered).
  8. Return to the start of sides a and b and take a step of 0.1 along side c (position 0,0,0.1).
  9. Repeat 3-8 until you reach the end of sides a, b and c (i.e. points 0...1,0...1,0...1 have been considered and position 1,1,1 reached)

I am trying to use python to output the list of each position, by defining loops and functions but it stops once I get to x=0, y=0 z=1.0. It appears to me that two of my while loops are not working....

The code I have thus far is below:

def zcoord():
    global z
    while z<=1.0:        
        print('%.2f' % x + ' %.2f' % y + ' %.2f' % z)
        z+=0.1

def ycoord():
    global y
    while y<=1.0:
        zcoord()
        y+=0.1

def xcoord():
    global x
    while x<=1.0:
        ycoord()
        x+=0.1
x=0.0
y=0.0
z=0.0
xcoord()
2
  • Hint: What happens to the z variable after y+=0.1? Commented Aug 19, 2013 at 11:46
  • As you are new to programming: it is quite bad stype to use globals for this task. If you pass the relevant data via arguments and initialize the data not coming from outside at the top of the function, your original problem is solved as well. Commented Aug 19, 2013 at 11:48

1 Answer 1

3

Your loops are all working, but your zcoord() function only prints one loop.

The second time zcoord() is called, z is still greater than 1.0 and the loop won't run, and won't print.

If you wanted to create nested loops, then it'd be simpler using integers and divide by 10:

for x in range(11):
    for y in range(11):
        for z in range(11):
            print('%.2f' % (x/10.0) + ' %.2f' % (y/10.0) + ' %.2f' % (z/10.0))

This can be collapsed into one loop using itertools.product():

from itertools import product

for x, y, z in product(range(11), repeat=3):
    print('%.2f' % (x/10.0) + ' %.2f' % (y/10.0) + ' %.2f' % (z/10.0))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.