-1

I am trying to write a program which takes a list of directions and magnitudes and outputs the distance of the robot from its starting position.

I get an error when executing the following code but I cannot identify why I get the error.

import math

position = [0,0]
direction = ['+Y','-X','-Y','+X','-X','-Y','+X']
magnitude = [9,7,4,8,3,6,2]
i = 0

while i < len(direction):
    if direction[i] == '+Y': position[0] += magnitude[i]
    elif direction[i] == '-Y': position[0] -= magnitude[i]
    elif direction[i] == '+X': position[1] += magnitude[i]
    elif direction[i] == '-X': position[1] -= magnitude[i]
    else: pass
    i += 1

print float(math.sqrt(position[1]**2+position[0]**2))

Edit:

I get this error:

IndentationError: unindent does not match any outer indentation level

  • 2
    please post the error aswell – Ashoka Lella Jul 14 '14 at 05:41
  • 1
    I just ran this and got no error – yuvi Jul 14 '14 at 05:44
  • @user2357 You will probably get correct distance. But the position will be a bit wrong, as on lines with `+X` and `-X` you swapped `-=` and `+=`. – Jan Vlcinsky Jul 14 '14 at 05:46
  • Yes, I just noticed that mistake, I should have used up, down, etc. +x, -x etc. is a bit confusing. I don't get any distance at all though. –  Jul 14 '14 at 05:48
  • 1
    @user2357 you probably mixed tabs and spaces. Check out http://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level – Ashoka Lella Jul 14 '14 at 05:48
  • You're mixing tabs and spaces. You use spaces to begin with, but switch to tabs for the `if` part. Use spaces throughout. – Jayanth Koushik Jul 14 '14 at 05:49
  • Thank you, I didn't know that I could get an error from mixing tabs and spaces. Good to know. –  Jul 14 '14 at 05:51
  • Fixed. I get an answer now! Thank you. –  Jul 14 '14 at 05:52

2 Answers2

0

Most probably you have mixed up your spaces and tabs. In this instance, it might be easier to put the sign within the magnitude and filter with x and y like so:

In [15]: mDr = [ (int(d[0]+m), d[1]) for (d, m) in zip(direction, map(str, magnitude))]

In [16]: mDr
Out[16]: [(9, 'Y'), (-7, 'X'), (-4, 'Y'), (8, 'X'), (-3, 'X'), (-6, 'Y'), (2, 'X')]

In this case, you can get to total x and y distances pretty easily. For example, the y distances:

In [17]: [md[0]  for md in mDr if md[1] =='Y']
Out[17]: [9, -4, -6]

And the total y distance in the particular direction:

In [18]: sum( [md[0]  for md in mDr if md[1] =='Y'] )
Out[18]: -1

You can do the same for x and then calculate the distance that way.

ssm
  • 5,277
  • 1
  • 24
  • 42
-1

Here comes my offtopic reaction (your problem was mixing tabs and spaces, my answer is simple rewrite).

import math

xymoves = {"+X": (1, 0), "-X": (-1, 0), "+Y": (0, 1), "-Y": (0, -1)}

position = [0, 0]
directions = ['+Y', '-X', '-Y', '+X', '-X', '-Y', '+X']
assert all(xymove in xymoves for xymove in directions)
magnitudes = [9, 7, 4, 8, 3, 6, 2]

for direction, magnitude in zip(directions, magnitudes):
    xmove, ymove = xymoves[direction]
    position[0] += magnitude * xmove
    position[1] += magnitude * ymove

print math.sqrt(position[1]**2+position[0]**2)

Changes:

  • looping using for and not while with incrementing index.
  • the logic "where to move" moved from if elif elif into dictionary xymoves
  • rejecting to process direction, which is not expected
  • math.sqrt always returns float, so conversion to float removed

Note, that the dictionary with xymoves could be extended with other directions, e.g. using "N" for North, "NE" for North-East etc.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98