48 lines
889 B
Go
48 lines
889 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"math/rand"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func guess() {
|
||
|
numberToGuess := rand.Intn(100) + 1
|
||
|
|
||
|
for i := 10; i > 0; i-- {
|
||
|
|
||
|
fmt.Println("Please guess a number from 1 to 100")
|
||
|
fmt.Printf("You have %v guesses left.\n", i)
|
||
|
reader := bufio.NewReader(os.Stdin)
|
||
|
input, err := reader.ReadString('\n')
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
input = strings.TrimSpace(input)
|
||
|
guess, err := strconv.Atoi(input)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
if guess == numberToGuess {
|
||
|
fmt.Println("Great job! You guessed it!")
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
if guess > numberToGuess {
|
||
|
fmt.Println("Oops. Your guess was HIGH.")
|
||
|
continue
|
||
|
}
|
||
|
if guess < numberToGuess {
|
||
|
fmt.Println("Oops. Your guess was LOW.")
|
||
|
continue
|
||
|
}
|
||
|
if i == 1 {
|
||
|
fmt.Printf("Sorry. You didn't guess my number. It was: %v\n", numberToGuess)
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
}
|
||
|
}
|