【问题标题】:python calculate distance with height angle and original velocitypython用高度角和原始速度计算距离
【发布时间】:2017-08-29 07:07:11
【问题描述】:

我正在编写 python 代码,如果给出了高度、角度和原始速度,我正在尝试计算物体的距离。这是我的代码。它不起作用,每次都会给我一条错误消息。

import subprocess as sp
import math
sp.call('cls',shell=True)
pangle = float(0.0)
distance = float(0)
a = float(0)
y = float(0)
v = float(0)
a = input("Angle:")
y = input("Hight (Meter):")
v = input("Speed (M/S):")
try:
    a = float(angle)
    y = float(hight)
    v = float(speed)
except:
    sp.call('cls',shell=True)
    print("Error")
    error = input("")
    exit
distance = float((v * (math.cos(math.radians(a/1))))*(v * math.sin(math.radians(a)) + ((v * math.sin(math.radians(a)))^2+2*y)**(1.0/2)))
sdistance = str(distance)
print ("Distance is " + sdistance + " Meter")
error = input("")
exit

请帮忙

【问题讨论】:

  • 你还没有定义angleheightspeed
  • 它会给你什么错误?请提供。
  • 添加足够的代码,以便您的 sn-p 可以运行。还要为您的 sn-p 导致的错误提供整个回溯。见How to create a Minimal, Complete, and Verifiable example。此外,更准确地定义您的术语“高度、角度和原始速度”。

标签: python math distance trigonometry computation


【解决方案1】:

你的try块应该是这样的:

try:
    a = float(a)
    y = float(y)
    v = float(v)

注意^不是python中的指数运算符,请改用**

distance = float((v * (math.cos(math.radians(a/1))))*(v * math.sin(math.radians(a)) + ((v * math.sin(math.radians(a)))**2+2*y)**(1.0/2)))

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2021-05-28
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多