Created
July 18, 2018 08:58
-
-
Save solen003/6ca125a54fd2bb35a958749570814aa7 to your computer and use it in GitHub Desktop.
A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following: UP 5 DOWN 3 LEFT 3 RIGHT 2 ¡ The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequen…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
input
Give input like this
UP 26 then enter DOWN 26 then enter RIGHT 26 then enter LEFT 36 enter pass "" it will break out from loop and give you the distance.
Hope it is useful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to get input
How give input