[Tutor] Order Of Operations Question
evert.rol at gmail.com
Wed Jul 28 12:11:41 CEST 2010
Sorry, forgot to reply-to-all:
I don't know the book nor the exercise, but see my comments interspersed in the code, and a few general remarks at the bottom
> From a practice exercise in Building Skills In Python page 64 I'm
> working on How Much Does The Atmosphere Weigh? Part 1:
> To check it states that the answer should be app. 10**18kg However,
> and I've checked to make sure that the math I've layed out matches up
> with the texts, I get 5.07360705863e+20
> In the code I have broken the order of operations down to more
> parenthetical, and tried outright, but see nothing obvious about how
> it's strung together. If anyone has had a similar experience with the
> problem given, or see anything blatantly obvious I've done wrong with
> the ordering of operations. I tried to include as much of the
> problem(formulas are just above variables they're used in) as comments
> as possible.
> import math
> def atmosphereWeight():
> pi = math.pi
> """Air Pressure (at sea level) P0. This is the long-term average.
> P0 = 1.01325 × 10**5"""
> airPressCLevl = 1.01325*(10**5)
> gravity = 9.82
> """We can use g to get the kg of mass from the force of air
> pressure P0. Apply the acceleration of gravity
> (in m/sec2) to the air pressure (in kg · m/sec2). This result is
> mass of the atmosphere in kilograms per
> square meter (kg/m2).
> Mm2 = P0 × g"""
> masAtmoInKgPerSqM = airPressCLevl * gravity
Simply from looking at the units left and right of the equality sign, you'll need to *divide* by g, not multiply: [kg] = [kg m / s^2] / [m / s^2]
> """Given the mass of air per square meter, we need to know how
> many square meters of surface to apply
> this mass to. Radius of Earth R in meters, m. This is an average
> radius; our planet isn’t a perfect sphere.
> R = 6.37 × 10"""
> avgRadiusEarth = 6.37 * (10**6)
> """The area of a Sphere.
> A = 4πr2"""
> areaSphere = 4 * pi * (avgRadiusEarth**2)
> """Mass of atmosphere (in Kg) is the weight per square meter,
> times the number of square meters
> Ma = P0 × g × A"""
> masEarthAtmoInKgPerSqM = airPressCLevl * gravity * areaSphere
ditto here: divide by gravity, not multiply by it.
Few general remarks:
- the standard way of writing numbers with a power of ten in code is something like 1.01325e5. I guess this is also easier/quicker to execute (not that this code is time-critical, but in general)
- why do you assign masTmoInKgPerSqM, then later not use it when calculating masEarthAtmoInKgPerSqM?
- just use math.pi when calculating areaSphere, instead of "pi = math.pi" and then later using pi. For me, that's just as clear.
- no need to put parentheses around powers; they are evaluated before the multiplication (unless this is what you meant by "to more parenthetical"
- try indenting the comments as well; more readable
Probably not all of the above are necessary, if you wrote this for debugging your problem, but they're just some thoughts that occurred to me.
Enjoy your calculations.
More information about the Tutor