r/pythonhelp • u/Microversonauta • Dec 23 '22
Calculating weight on planets
Hello,
I am trying to learn python on my own and i am having a difficult time with this exercise where I have to calculate the weight of a person in a x planet, can you give some guidance pls?
Thank you all in advance.
The exercise goes like this:
"Due to the difference in gravity on each celestial body, your weight is nowhere the same as on Earth. on
on some celestial bodies the gravity is greater and you therefore weigh more, on others the gravity is less
and you weigh less.
Which celestial body is best for you to move to to achieve your ideal weight? Enter your weight, choose one
celestial body and perform your mass on the chosen celestial body.
your mass on the moon = weight x 0.166
your mass on Jupiter = weight x 2.36
your mass on Mars = weight x 0.37
your mass on Venus = weight x 0.9
your mass on Neptune = weight x 1.12
parameters
The weight is a decimal number.
The celestial body is entered correctly: moon with a small letter, Jupiter, Mars, Venus and Neptune with
one upper case letter.
For any other celestial object or incorrect spelling, you display an error message."
I can get any further than this:
weight = float(input("Give your weight in(kg): "))
planet = input("In which planet are you? ")
#moon * 0.166
#Jupiter * 2.36
#Mart * 0.37
#Venus * 0.9
#Neptune * 1.12
if planet == moon:
weight = weight * 0.166
print("At " + str(planet) + " your weight is " + str(weight) + " kg")
I know is not far completed but wanted to test if it calculated correctly en it doesn't.
Result is:
Give your weight in(kg): 90
In which planet are you? moon
At moon your weight is 90.0 kg
2
Jan 15 '23
mass = float(input("Mass in kg: "))
moon = 1.62
earth = 9.81
(add the planets you want with the gravity)
planet = input("Planet: ")
if planet == "moon":
print("Weight: ", mass*moon, " ")
if planet == "earth":
print("Weight: ", mass*earth, " ")0
Dec 24 '22 edited Dec 24 '22
I would use Newton's Law of Universal Gravitation for this F= Gm1m2/r^2. If you know the radius of the planet, and the mass of the planet (you can store these as entries in a dictionary. Then you can calculate someone's weight on any planet as long as you know its mass and radius.
planet_dict = {
'Jupiter':[69910000,1.898*10**27],
'moon':[radius,mass],
'saturn':[radius,mass],
}
G = 6.67*10**-11
planet = input('Planet?')
m2=float(input('Input your mass'))
r,m1 = planet_dict[planet]
print(G*m1*m2/r**2)
I would also try out using a try/except block when taking your inputs. You can use try to float your input and check to see if your planet is in the dictionary. If the float fails or the dictionary key isn't found you can throw an error telling the user how to correct it. Also, what I wrote above gives the weight, not the mass. If you want to take in kg and print out kg, you will need to divide the result by 9.8.
1
u/bulaybil Dec 24 '22
Yeah definitely don’t do that. Two more data points and one more calculation Instead of a simple coefficient is just pointless.
1
Dec 24 '22
It gives a more reliable result, it's how those other numbers OP listed were calculated, and OP mentioned they were in a physics class, so I gave them a physics class answer to the problem and started from close to first principals.
2
u/Microversonauta Dec 23 '22
Made it work, i'll leave the solution here in case someone else has te same problem but I don't know if it's the most correct way to do this
gewicht = float(input("Geef je gewicht in (kg): "))
hemellichaam = input("Op welk hemellichaam ben je? ")
massa = 0
#maan = gewicht * 0.166
#Jupiter = gewicht * 2.36
#Mars = gewicht * 0.37
#Venus = gewicht * 0.9
#Neptunus = gewicht * 1.12
if hemellichaam == "maan":
massa = gewicht * 0.166
print("Op " + str(hemellichaam) + " weeg je " + str(massa) + " kilogram")
elif hemellichaam == "Jupiter":
massa = gewicht * 2.36
print("Op " + str(hemellichaam) + " weeg je " + str(massa) + " kilogram")
elif hemellichaam == "Mars":
massa = gewicht * 0.37
print("Op " + str(hemellichaam) + " weeg je " + str(massa) + " kilogram")
elif hemellichaam == "Venus":
massa = gewicht * 0.9
print("Op " + str(hemellichaam) + " weeg je " + str(massa) + " kilogram")
elif hemellichaam == "Neptunus":
massa = gewicht * 1.12
print("Op " + str(hemellichaam) + " weeg je " + str(massa) + " kilogram")
else:
print("Ongeldige invoer!")