Kodewick
Start learning →
Basic←→0 / 70

Bus card

#058BasicErrorsConditions4 min

The bus card holds balance and this ride costs fare. Write Pay(balance, fare) to return what's left and nil if there's enough, or 0 and the error "not enough money" if not. Make the error with errors.New, and remember to import errors first.

Examples
Pay(20, 3)→(17, None)
Pay(2, 3)→(0, 'not enough money')
The functionfunc Pay(balance, fare int) (int, error)
main.go
package main

// your code here: import "errors"

func Pay(balance, fare int) (int, error) {
	if balance < fare {
		// your code here: return 0 and the error "not enough money"
	}
	return balance - fare, nil
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.