Course outline
Course / The terminal and the language

Ticket price with early exits

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/10-05-ticket-price
cd 10-05-ticket-price
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal Price a museum ticket by checking the special cases first, in order, and returning as soon as the price is known — no else anywhere.

Example If you needed a similar tiered parking fee, you'd write:

func ParkingFee() int {
	if Hours < 0 {
		return -1
	}
	if Hours == 0 {
		return 0
	}
	if Member {
		return 3
	}
	return 10
}

Each case gets its own if and its own return. Once one matches, the function is done — nothing below it runs.

Rules

  • Edit price.go. Price has a // TODO comment and a return line you will change.
  • Check the cases in exactly this order, each with its own early return:
    1. A negative Age is invalid: return -1.
    2. Under 3 years old: return 0.
    3. A member: return 5.
    4. Under 12, or 65 and over: return 8.
    5. Everyone else: return 12.
  • Write every case as its own if with a return. Do not use else anywhere in the function.
  • Do not change the var declarations or the Price function signature. Do not add imports.

What you'll practice Ordering guard clauses so the strictest case is checked first, and writing them as early returns instead of an if / else chain.

Run it

gonomicon fetch week-1/10-05-ticket-price
cd 10-05-ticket-price
gonomicon test
gonomicon submit

Hints

  1. Check the special cases from strictest to loosest, in order, and return out of each one — negative age first, then under 3, then membership, then the age discount, then the ordinary price last.
  2. Each check is its own "if" with its own "return" and no "else" anywhere, the same shape as the lesson's ticket() function.
  3. if Age < 0 { return -1 } if Age < 3 { return 0 } if Member { return 5 } if Age < 12 || Age >= 65 { return 8 } return 12

Something wrong with this lesson? Report a problem