DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #247 - Get Planet Name By ID

The function is not returning the correct values. Can you figure out why?

def get_planet_name(id):
    # This doesn't work; Fix it!
    name=""
    switch id:
        case 1: name = "Mercury"
        case 2: name = "Venus"
        case 3: name = "Earth"
        case 4: name = "Mars"
        case 5: name = "Jupiter"
        case 6: name = "Saturn"
        case 7: name = "Uranus"  
        case 8: name = "Neptune"
    return name

Example

get_planet_name(3) # should return 'Earth'

Tests

get_planet_name(3)
get_planet_name(8)
get_planet_name(1)
get_planet_name(5)

Good luck!


This challenge comes from jhoffner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (6)

Collapse
 
aminnairi profile image
Amin • • Edited

Because the interpreter is not helpful.

Try Elm, the compiler is nice.

type Planet
    = Mercury
    | Venus
    | Earth
    | Mars
    | Jupiter
    | Saturn
    | Uranus
    | Neptune


getPlanetName : Planet -> String
getPlanetName planet =
    case planet of
        Mercury -> "Mercury"
        Venus   -> "Venus"
        Earth   -> "Earth"
        Mars    -> "Mars"
        Jupiter -> "Jupiter"
        Saturn  -> "Saturn"
        Uranus  -> "Uranus"
        Neptune -> "Neptune"

Note: Does not respond to the challenge at all

Collapse
 
craigmc08 profile image
Craig McIlwrath •

Same as #233

Collapse
 
sagartyagi121 profile image
Gajender Tyagi •

No break statement

Collapse
 
mgautam98 profile image
Gautam Mishra •

It will always give "Neptune" as answer because there is no break statement.

Collapse
 
jbristow profile image
Jon Bristow •

Fallthrough switches are second only to null values as far as bad programming language ideas go.

Collapse
 
marlo22 profile image
marcin93 • • Edited

I don't know Python but as a JavaScript developer I also thought about lack of break statements in this code.