pythonbook/Python 基础教程/1.5.7 E练习5.py

30 lines
1000 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 5.机器人从原点0,0开始在平面中移动。 机器人可以通过给定的步骤向上,向下,向左和向右移动。
# 机器人运动的痕迹如下所示: UP 5 DOWN 3 LETF 3 RIGHT 2 方向之后的数字是步骤。
# 请编写一个程序来计算一系列运动和原点之后距当前位置的距离。
# 如果距离是浮点数,则只打印最接近的整数。
# 例:如果给出以下元组作为程序的输入:
# UP 5 DOWN 3 LETF 3 RIGHT 2 然后程序的输出应该是2
import math
pos = [0, 0]
print("请输入:")
while True:
s = input()
if not s:
break
movement = s.split(" ")
direction = movement[0]
steps = int(movement[1])
if direction == "UP":
pos[0] += steps
elif direction == "DOWN":
pos[0] -= steps
elif direction == "LEFT":
pos[1] -= steps
elif direction == "RIGHT":
pos[1] += steps
else:
pass
print(int(round(math.sqrt(pos[1] ** 2 + pos[0] ** 2))))