Need help on Learn Go Functions: Interstellar Travel

Hi everyone, first time posting in the forums here so forgive me if this is a duplicate - I did try searching but wasn’t finding any similar posts.
My Interstellar Travel code works for the project as long as I use one of the designated Mercury, Mars, or Venus options, but if I use something like “Jupiter” as an input for planetChoice, it still gives me the greetPlanet message instead of cantFly.

When I’ve adjusted the default option in the switch to be 1000001 instead of 1000000, it seems to work as expected. I even went through the follow-along video and did my best to make sure I had everything right, but I feel I must be missing something. Any help appreciated!

package main import "fmt" // Create the function fuelGauge() here func fuelGauge(fuel int) { fmt.Println("You have", fuel, "units of fuel remaining") } // Create the function calculateFuel() here func calculateFuel(planet string) int { var fuel int switch planet { case "Venus": fuel = 300000 case "Mercury": fuel = 500000 case "Mars": fuel = 700000 default: fuel = 0 } return fuel } // Create the function greetPlanet() here func greetPlanet(planet string) { fmt.Println("Welcome to planet", planet) } // Create the function cantFly() here func cantFly() { fmt.Println("We do not have the available fuel to fly there.") } // Create the function flyToPlanet() here func flyToPlanet(planet string, fuel int) int { var fuelRemaining, fuelCost int fuelRemaining = fuel fuelCost = calculateFuel(planet) if fuelRemaining >= fuelCost { greetPlanet(planet) fuelRemaining -= fuelCost } else { cantFly() } return fuelRemaining } func main() { // Test your functions! // Create `planetChoice` and `fuel` var fuel int fuel = 1000000 var planetChoice string planetChoice = "Jupiter" fuel = flyToPlanet(planetChoice, fuel) fuelGauge(fuel) // And then liftoff! }

There is nothing wrong with your code. If you follow the steps of the project, then you will end up with the code you posted and see the behavior you have noticed. Simply put, the steps specified in the project don’t properly handle planets other than Mercury, Mars & Venus. If a planet other than the three planets is provided, then according to the switch statement, required fuel will be 0. Since the condition fuelRemaining >= fuelCost will be true, so greetPlanet will be called and we will arrive at the destination without spending any fuel. If we don’t want the greeting to be printed, then we could use an if statement e.g.

func flyToPlanet(planet string, fuel int) int {
  var fuelRemaining, fuelCost int
  fuelRemaining = fuel
  fuelCost = calculateFuel(planet)

  if fuelCost == 0 {
      return fuelRemaining

  if fuelRemaining >= fuelCost {

where invalidPlanet could be a function which prints out some appropriate message. Or, you could implement some other way to handle other planets. The steps of the project result in the behavior you mentioned. There is nothing wrong in how you have followed the steps.

1 Like

Thank you so much for the feedback and help!

1 Like