Unit 2 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the problem asking for?
Q: What are the inputs?
planets
mapping planet names to dictionaries containing the number of moons and orbital period, and a string planet_name
.Q: What are the outputs?
Q: Are there any constraints on the dictionary values?
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.
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."