-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (47 loc) · 1.01 KB
/
main.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
54
55
56
57
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/fatih/color"
)
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
func main() {
red := color.New(color.FgRed, color.Bold).PrintFunc()
yellow := color.New(color.FgYellow).PrintFunc()
green := color.New(color.FgGreen).PrintFunc()
rand.Seed(time.Now().UnixNano())
randomNum := randInt(0, 100)
tries := 0
for {
green("Enter guess: ")
var guess string
fmt.Scanln(&guess)
if guess == "q" || guess == "" {
break
}
guessInt64, err := strconv.ParseInt(guess, 10, 8)
if err != nil {
red("Invalid input\n")
continue
}
var guessInt int = int(guessInt64)
if !(guessInt >= 0 && guessInt <= 100) {
red("Input must be between 0 and 100\n")
continue
} else {
tries++
}
if guessInt > randomNum {
yellow("Try a smaller number\n")
} else if guessInt < randomNum {
yellow("Try a larger number\n")
} else {
green("Correct! You got it in ", tries, " tries! 🎉 \n")
break
}
}
}