// Chapter 20 example: read scores until the word done, then average them. // Run with: go run exercises/20/03-sentinel.go package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) total := 0 count := 0 for { fmt.Print("Score from 1 to 100, or done: ") text, _ := reader.ReadString('\n') answer := strings.TrimSpace(text) if answer == "done" { break } score, _ := strconv.Atoi(answer) if score < 1 || score > 100 { fmt.Println("Skipped.") continue } total += score count++ } if count > 0 { average := float64(total) / float64(count) fmt.Printf("%d scores, average %.2f\n", count, average) } else { fmt.Println("No scores entered.") } }