Monday, April 15, 2024
HomeSoftware EngineeringThe right way to Get Planet Identify By ID in Python

# The right way to Get Planet Identify By ID in Python

## The problem#

Create a perform that takes in an `id` and returns the `planet title`.

## The answer in Python code#

Possibility 1:

``````def get_planet_name(id):
return {
1: "Mercury",
2: "Venus",
3: "Earth",
4: "Mars",
5: "Jupiter",
6: "Saturn",
7: "Uranus" ,
8: "Neptune"
}[id]
``````

Possibility 2:

``````def get_planet_name(id):
return ["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"][id-1]
``````

Possibility 3:

``````def get_planet_name(id):
if   id == 1: return "Mercury"
elif id == 2: return "Venus"
elif id == 3: return "Earth"
elif id == 4: return "Mars"
elif id == 5: return "Jupiter"
elif id == 6: return "Saturn"
elif id == 7: return "Uranus"
elif id == 8: return "Neptune"
``````

## Check circumstances to validate our answer#

``````import check

@check.describe("Mounted Checks")
def fixed_tests():
@check.it('Fundamental Check Instances')
def basic_test_cases():
check.assert_equals(get_planet_name(2), 'Venus')
check.assert_equals(get_planet_name(5), 'Jupiter')
check.assert_equals(get_planet_name(3), 'Earth')
check.assert_equals(get_planet_name(4), 'Mars')
check.assert_equals(get_planet_name(8), 'Neptune')
check.assert_equals(get_planet_name(1), 'Mercury')
``````
RELATED ARTICLES