-
Notifications
You must be signed in to change notification settings - Fork 0
/
17-guess-game.go
53 lines (45 loc) · 938 Bytes
/
17-guess-game.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
53
/**
* guess-game.go
*
* Guess a number between 1-100
*
*/
package main
import (
"fmt"
"math/rand"
"time"
)
// automatically called on start
func init() {
// new random seed
rand.Seed(time.Now().UnixNano())
}
func main() {
// variable to store guess
var guess int
// variable to store number of guesses
var count int
// pick number to guess, add 1 since Intn gives [0,99]
num := rand.Intn(100) + 1
fmt.Println(">> I'm thinking of a number between 1-100 << ")
// guessing loop
for {
// prompt user for guess
fmt.Print("Guess: ")
_, err := fmt.Scanf("%d", &guess)
if err == nil {
count += 1 // increment guess counter
if guess > num {
fmt.Println(" >> Too high ")
} else if guess < num {
fmt.Println(" >> Too low ")
} else {
fmt.Printf("Correct! It took you %d guesses!\n", count)
break
}
} else { // an error with input
fmt.Println(">> Please input a number")
}
}
}