Kodewick
Start learning →
Basic←→0 / 70

How many cups?

#057BasicErrorsStrings4 min

In the tea shop's order box, customers type how many cups they want. Cups(text) already turns the text into a number with strconv.Atoi and returns an error if it isn't one. Add the other check: fewer than 1 cup is no good either, so return 0 and the error "at least 1 cup".

Examples
Cups("3")→(3, None)
Cups("two")→(0, 'not a number')
Cups("0")→(0, 'at least 1 cup')
The functionfunc Cups(text string) (int, error)
main.go
package main

import (
	"errors"
	"strconv"
)

func Cups(text string) (int, error) {
	n, err := strconv.Atoi(text)
	if err != nil {
		return 0, errors.New("not a number")
	}
	// your code here: if n is less than 1, return 0 and the error "at least 1 cup"
	return n, nil
}
Ctrl + Enter runs too
Result

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