Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lnugraha authored May 30, 2024
1 parent cdffe46 commit e419e0f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 2024_05_31_DataStructures/planet_conditional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import math

def main():
MERCURY_GRAVITY = (37.6 / 100)
VENUS_GRAVITY = (88.9 / 100)
MARS_GRAVITY = (37.8 / 100)
JUPITER_GRAVITY = (236 / 100)
SATURN_GRAVITY = (108.1 / 100)
URANUS_GRAVITY = (81.5 / 100)
NEPTUNE_GRAVITY = (114 / 100)

earthWeight = float(input("Enter the object weight: "))
planetName = input("Enter a planet name: ")

match planetName:
case "Mercury":
planetWeight = float(earthWeight) * MERCURY_GRAVITY

case "Venus":
planetWeight = float(earthWeight) * VENUS_GRAVITY

case "Mars":
planetWeight = float(earthWeight) * MARS_GRAVITY

case "Jupiter":
planetWeight = float(earthWeight) * JUPITER_GRAVITY

case "Saturn":
planetWeight = float(earthWeight) * SATURN_GRAVITY

case "Uranus":
planetWeight = float(earthWeight) * URANUS_GRAVITY

case "Neptune":
planetWeight = float(earthWeight) * NEPTUNE_GRAVITY

case "Earth":
planetWeight = float(earthWeight)

case other:
planetWeight = -1.0 # Why put a negative value here?

print("The name of the planet: " + planetName + " with weight: " + str(planetWeight))

if __name__ == '__main__':
main()
36 changes: 36 additions & 0 deletions 2024_05_31_DataStructures/planet_dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
planet_dict = [
{"mercury": 0.376},
{"venus": 0.889},
{"mars": 0.378},
{"jupiter": 2.36},
{"saturn": 1.081},
{"uranus": 0.815},
{"neptune": 1.14}
]

def calculate_weight(target_planet, earth_weight):
# TODO: Check the earth weight, make sure >= 0 and numbers only
# TODO: Check that the target planet name is listed in the dictionary, otherwise return an error

planet_name = target_planet.lower()
planet_constant = -1.0
for i in range( len(planet_dict) ):
for name, gravity in planet_dict[i].items():
if (planet_name == name):
planet_constant = gravity
else:
return -1.0
planet_weight = planet_constant * earth_weight

return planet_weight

def main():
earth_weight = float(input("Enter the object weight: "))
planet_name = input("Enter a planet name: ")

planet_weight = calculate_weight(planet_name, earth_weight)

print("The name of the planet: " + planet_name + " with weight: " + str(planet_weight))

if __name__ == '__main__':
main()

0 comments on commit e419e0f

Please sign in to comment.