Python:尝试将结果和角度转换为原始 i 和 j 值。

我试图让 python 取一个结果值和角度值来返回向量的原始 i 和 j 值。我可以让它从 i 和 j 值到结果和角度,但反过来就很棘手。


    #vector conversion (x,y) to resultant and angle#

import math


menu = input('Vector Conversion: for vector to (x,y) press 1. \n                   for (x,y) to vector press 2.')

if menu == '2':

    user_distancex = float(input('What is the distance in x-direction?'))

    user_distancey = float(input('What is the distance in y-direction?'))


    r = (math.sqrt(user_distancex**2 + user_distancey**2))  

    theta = math.atan(user_distancey/user_distancex)*(180/math.pi)


    print(r, 'feet',theta, 'degrees')

elif menu == '1':


    user_angle = float(input('What is the angle of your vector?'))

    user_resultant = float(input('What is the distance (resultant) of your vector'))

    x_dist = user_resultant*math.cos(math.degrees(user_angle))

    y_dist = user_resultant*math.sin(math.degrees(user_angle))

    print((x_dist,y_dist))


德玛西亚99
浏览 214回答 1
1回答

慕田峪7331174

math.degrees(user_angle)将您的用户角度从弧度转换为度数。但是如果用户以度数输入它,它已经是度数了!你应该math.radians(user_angle)改用。它还有助于澄清您的输入提示:user_angle = float(input('What is the angle of your vector (in degrees)?'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python