Course / The terminal and the language
Ticket price with early exits
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitGoal
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.Pricehas a// TODOcomment and a return line you will change. - Check the cases in exactly this order, each with its own early return:
- A negative
Ageis invalid: return-1. - Under
3years old: return0. - A member: return
5. - Under
12, or65and over: return8. - Everyone else: return
12.
- A negative
- Write every case as its own
ifwith areturn. Do not useelseanywhere in the function. - Do not change the
vardeclarations or thePricefunction 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
- 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.
- Each check is its own "if" with its own "return" and no "else" anywhere, the same shape as the lesson's ticket() function.
- if Age < 0 { return -1 } if Age < 3 { return 0 } if Member { return 5 } if Age < 12 || Age >= 65 { return 8 } return 12