-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess.go
executable file
·60 lines (47 loc) · 1.01 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
53
54
55
56
57
58
59
60
package main
// 1. make the program print out how many tries it took
// 2. See if you can tell the user is lying
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
low := 1
high := 100
fmt.Println("Please think of a number between", low, " and ", high)
fmt.Println("Press enter when ready")
scanner.Scan()
// n == 1 to 100 O(n)
guess := 50
lastguess := 0
tries := 1
for {
fmt.Println("I guess the number is ", guess)
fmt.Println("Is that :")
fmt.Println("(a) too high?")
fmt.Println("(b) too low?")
fmt.Println("(c) correct")
scanner.Scan()
response := scanner.Text()
if response == "a" {
high = guess
guess = (guess + low) / 2
} else if response == "b" {
low = guess
guess = (guess + high) / 2
} else if response == "c" {
fmt.Println("I won!", "Number of tries ", tries)
break
} else {
fmt.Println("Invalid response, try again.")
}
if guess == lastguess {
fmt.Println("Liar!!!")
break
}
lastguess = guess
tries++
}
}