-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess.go
52 lines (48 loc) · 1.02 KB
/
guess.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func guess() {
success := false
seconds := time.Now().Unix()
rand.Seed(seconds)
target := rand.Intn(100) + 1
fmt.Println("I have chosen random number between 1 and 100.")
fmt.Println("Can you guess it?")
for x := 10; x > 0; x-- {
//Taking input
reader := bufio.NewReader(os.Stdin)
fmt.Print("Make a guess: ")
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 < target {
fmt.Println("Oops. Your guess was LOW")
fmt.Println("You have ", x-1, " gueses left.")
} else if guess > target {
fmt.Println("Oops. Your target was HIGH")
fmt.Println("You have ", x-1, " gueses left.")
} else {
success = true
break
}
}
if success == true {
fmt.Println("Good Job! You guessed it!")
} else {
fmt.Println("Sorry You didn't guess my number. It was: ", target)
}
}