Python方程式 - 错误的答案

时间:2013-10-17 02:04:03

标签: python equation

我在使用这个等式时遇到了麻烦。

from __future__ import division
import math

pi = 3.14159265
g = 6.67428*(10**-11)

user_circum = raw_input("Circumference (km) of planet? ")
user_acc = raw_input("Acceleration due to gravity (m/s^2)?")

def display_results(radius , mass , velocity):
    print "Radius of the planet"  , radius ,"km"
    print "Mass of the planet" , float(mass/10**15) ,"(10^21 kg)"
    print "Escape velocity of the planet" , velocity/1000 , "(km/s)"

def escape_velocity(circumference , acceleration):
    circumference = float(circumference)
    acceleration = float(acceleration)
    radius = circumference/(2*pi)
    mass = (acceleration * radius ** 2)/g
    vEscape = ((2*g*mass)/radius) ** 0.5
    display_results(radius , mass , vEscape)

escape_velocity(user_circum, user_acc)

用户输入38000和9.8:

Circumference (km) of planet? 38000
Acceleration due to gravity (m/s^2)? 9.8

我应该得到这个答案:

Calculating the escape velocity...
Planet radius = 6047.9 km
Planet mass = 5370.7 x 10^21 kg
Escape velocity = 10.9 km/s

但我得到逃生速度的答案。前两个是正确的。

Escape velocity of the planet 0.344294353352 (km/s)

这是逃逸速度√2gm/ r的公式 知道如何解决这个问题吗?非常感谢。 我怎么能将我的答案舍入到一个小数位?感谢。

3 个答案:

答案 0 :(得分:3)

我认为你正在混淆单位。用户以公里为单位输入周长,但您使用的引力常数以米为单位。如果我以米为单位输入周长,则逃逸速度会给出您期望的结果:

Circumference (km) of planet? 38000000
Acceleration due to gravity (m/s^2)?9.8
Radius of the planet 6047887.8444 km
Mass of the planet 5370677950.42 (10^21 kg)
Escape velocity of the planet 10.8875434213 (km/s)

当然,这两个第一个结果不再好了。因此,要么调整引力常数以使其以公里为单位,要么更好,总是使用米。为方便起见,您可以随时要求用户输入以千米为单位的周长,并自行将其转换为米。

答案 1 :(得分:1)

如果你有一个图书馆为你管理单位,那么你的生活会更容易。这样的事情会给你一个启动:

from collections import Counter

class UnitsThing(object):
    UNITS = ["kg", "km", "m", "s"]
    def __init__(self, measure, units):
        self.measure = measure
        if type(units) is str:
            if "/" not in units:
                over, under = units, ""
            else:
                over, under = units.split("/")
            o, u = self.unitize(over), self.unitize(under)
            o.subtract(u)
            self.units = o
        else:
            self.units = units
        p = self.units.get("km")
        if p:
            self.units["m"] += p
            self.units["km"] = 0
            self.measure *= 1000.0 ** p
    def __mul__(self, x):
        if type(x) is UnitsThing:
            new_units = self.units.copy()
            new_units.update(x.units)
            return UnitsThing(self.measure * x.measure, new_units)
        else:
            return UnitsThing(self.measure * x, self.units.copy())
    def __div__(self, x):
        if type(x) is UnitsThing:
            new_units = self.units.copy()
            new_units.subtract(x.units)
            return UnitsThing(self.measure / x.measure, new_units)
        else:
            print self.measure
            return UnitsThing(self.measure / x, self.units.copy())
    def __plus__(self, x):
        assert type(x) is UnitsThing
        assert x.units == self.units
        return UnitsThing(self.measure + x, self.units)
    def __sub__(self, x):
        assert type(x) is UnitsThing
        assert x.units == self.units
        return UnitsThing(self.measure - x, self.units)
    def unitize(self, units):
        c = Counter()
        for u in self.UNITS:
            while u in units:
                c[u] += 1
                i = units.index(u)
                units = units[:i] + units[i + len(u):]
        return c
    def unit_format(self, c):
        return "".join("{0}^{1}".format(k, v) for k, v in c.items() if v)
    def __str__(self):
        u = self.unit_format(self.units)
        return "{0} {1}".format(self.measure, u)
    def sqrt(self):
        return UnitsThing(self.measure ** 0.5, self.units)

然后你可以写这样的代码:

from math import pi

g = UnitsThing(6.67428e-11, "mmm/kgss")
user_circum = UnitsThing(40075, "km")
user_acc = UnitsThing(9.81, "m/ss")
radius = user_circum / (2 * pi)
mass = (user_acc * (radius * radius))/g
vEscape = ((g * mass * 2.0) / radius).sqrt()

你会得到这样的结果:

>>> print vEscape
11186.5542433 m^2s^-2

添加了github repo for this

答案 2 :(得分:0)

固定半径单位并在显示中进行校正,它看起来像:

from __future__ import print_function
from __future__ import division
from math import pi

G = 6.67428*(10**-11)

user_circum = raw_input("Circumference (km) of planet? ")
user_acc = raw_input("Acceleration due to gravity (m/s^2)?")

def display_results(radius, mass, velocity):
    print("Radius of the planet {:.1f}km".format(radius/1000))
    print("Mass of the planet {:.1f}(10^21 kg)".format(mass/10**21))
    print("Escape velocity of the planet {:.1f}(km.s)".format(velocity/1000))

def escape_velocity(circumference, acceleration):
    circumference = float(circumference)
    acceleration = float(acceleration)
    radius = circumference*1000/(2*pi)
    mass = (acceleration * radius ** 2)/G
    vEscape = ((2*G*mass)/radius) ** 0.5
    display_results(radius, mass, vEscape)                                                                                                                                     

escape_velocity(user_circum, use_acc)