Codepath

Space Encyclopedia

Unit 2 Session 1 (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the problem asking for?

    • A: The problem asks to return information about a planet from a dictionary if the planet exists, otherwise return a message indicating no data is available.
  • Q: What are the inputs?

    • A: A dictionary planets mapping planet names to dictionaries containing the number of moons and orbital period, and a string planet_name.
  • Q: What are the outputs?

    • A: A string providing the orbital period and number of moons for the planet, or a message indicating no data is available if the planet is not in the dictionary.
  • Q: Are there any constraints on the dictionary values?

    • A: The dictionary values for each planet contain "Orbital Period" and "Moons" keys.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Check if the planet exists in the dictionary and return the formatted string with its details, otherwise return the error message.

1. Check if `planet_name` is a key in the `planets` dictionary.
   - If true, extract the "Orbital Period" and "Moons" from the corresponding value.
   - Format and return the string with the planet's details.
2. If false, return the message indicating no data is available.

I-mplement

def planet_lookup(planets, planet_name):
    if planet_name in planets:
        planet_info = planets[planet_name]
        orbital_period = planet_info["Orbital Period"]
        moons = planet_info["Moons"]
        return f"Planet {planet_name} has an orbital period of {orbital_period} Earth days and has {moons} moons."
    else:
        return "Sorry, I have no data on that planet."
Fork me on GitHub