Distance calculator

0 votes

A robot moves in a plane starting from the origin point[0,0].. The robot can move toward UP, DOWN, LEFT, RIGHT. The trace of robot movements is as given following:

UP 5
DOWN 3
LEFT 3
RIGHT 2
The numbers after directions are steps. Write a program to compute the distance current position after sequence of movements.
Nov 13, 2020 in Python by Edureka
• 120 points
4,419 views

1 answer to this question.

0 votes

Hello @

Try this code:

import math

x, y = 0, 0

while True:
    step = input("Type in UP/DOWN/LEFT/RIGHT #step number: ")

    if step == "":
        break

    else:
        step = step.split(" ")

        if step[0] == "UP":
            y = y + int(step[1])
        elif step[0] == "DOWN":
            y = y - int(step[1])
        elif step[0] == "LEFT":
            x = x - int(step[1])
        elif step[0] == "RIGHT":
            x = x + int(step[1])

c = math.sqrt(x**2 + y**2)

print("Distance:", c)
answered Nov 13, 2020 by Niroj
• 82,840 points
its not working for me and let me know how should we give input
0 votes
dict= {"x":0, "y":0}
while True:
    a = input("enter your direction for the robot(UP, DOWN)etc: ")
    if a=="UP":
        dict["y"]+=5
    elif a=="DOWN":
        dict["y"]-=3
    elif a=="LEFT":
        dict["x"]-=3
    elif a=="RIGHT":
        dict["x"]+=2
    elif a=="DIST":
        d=math.sqrt(dict["x"]**2 + dict["y"]**2)
        print(d)
        break
    else:
        print("invalid entry, try again")
answered Apr 9, 2021 by Shammy

edited Mar 5
0 votes
Please check following code:

from math import sqrt

x0,y0 = 0,0

x1,y1 = 0,0

direction = ' '

while direction != "Calculate":

  if direction == "Up":

    y1=y1+steps

  elif direction == "Down":

    y1=y1-steps

  elif direction == "Right":

    x1=x1+steps

  elif direction == "Left":

    x1=x1-steps

  

  direction = input("Please enter direction as one of Up/Down/Left/Right or Calculate for finding distance: ")

  if direction != "Calculate":

    steps = float(input("Please enter steps: "))

if direction == "Calculate":

    sqrdist = (x1-x0)**2 + (y1-y0)**2

    distance = sqrt(sqrdist)

    print("Distance covered is: " + str(distance))
answered Jun 1, 2021 by Rohit

edited Mar 5
0 votes
#Starting position co-ordinates - x=0 and y=0
import math

x, y = 0, 0

while True:
    step = input("Type in UP/DOWN/LEFT/RIGHT (space) step number: ")
    step=step.upper()
    if step == "":

        
        break

    else:
        step = step.split(" ")
        
        if step[0] == "UP":
            y = y + int(step[1])
        elif step[0] == "DOWN":
            y = y - int(step[1])
        elif step[0] == "LEFT":
            x = x - int(step[1])
        elif step[0] == "RIGHT":
            x = x + int(step[1])

c = math.sqrt(x**2 + y**2)

print("Distance of current position from origin is :", c)
answered May 8, 2023 by Gopalan

edited Mar 5

Related Questions In Python

+1 vote
2 answers

Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

answered Aug 24, 2018 in Python by Omkar
• 69,220 points
10,674 views
0 votes
1 answer

Plot a k-distance graph in python

You probably want to use the matrix ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 58,020 points
5,119 views
0 votes
1 answer

Measuring the distance between pixels on OpenCv with Python

Assuming input frames will have "close to ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
5,798 views
0 votes
1 answer

Measuring the distance between pixels on OpenCv with Python

Assuming input frames will have "close to ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,840 views
+4 votes
2 answers

AWS Save & Share while using AWS Calculator

After doing your estimate, click the "Estimate ...READ MORE

answered Oct 11, 2018 in Cloud Computing by findingbugs
• 4,780 points
2,094 views
0 votes
1 answer

Use different distance formula other than euclidean distance in k means

K-means is based on variance minimization. The sum-of-variance formula ...READ MORE

answered Jun 21, 2018 in Data Analytics by Sahiti
• 6,370 points
1,750 views
+1 vote
2 answers

AWS: Long Distance Movement to S3 buckets

This description may interest you, check the ...READ MORE

answered Aug 30, 2018 in Cloud Computing by eatcodesleeprepeat
• 4,710 points
4,514 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP