Discuss / Python / 计算移动坐标

计算移动坐标

Topic source

Gussun

#1 Created at ... [Delete] [Delete and Lock User]

需要注意的是,input()获得的是str,不能够直接用于运算,需通过类型转化才可以。

import math

def move(x,y,step,angle=0):
#    if not isinstance(x,y,(int,float)):
#        raise TypeError('bad operand type')
    nx=x+step*math.cos(angle)
    ny=y+step*math.sin(angle)
    return nx,ny


x=float(input('请输入横坐标:'))
y=float(input('请输入纵坐标:'))
step=float(input('请输入移动步数:'))
angle=float(input('请输入移动方向:'))

nx,ny=move(x,y,step,angle)
print('nx=%.2f' %(nx),'ny=%.2f' %(ny))

  • 1

Reply