public
anonymous / guesser.go
Created

guesser.go

  • Download Gist
guesser.go
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
package main
 
import (
"fmt"
"math/rand"
"time"
)
 
func main() {
r := rand.New(rand.NewSource(time.Now().Unix()))
guess := (r.Intn(99))
var tries int
var input string
for input != "y" {
tries++
fmt.Println("Think of a number between 1 and 100. I will try to guess it. Is it ", guess+1, "?")
fmt.Print("[y] yes, [h] higher [l] lower > ")
fmt.Scanf("%v", &input)
if input == "l" {
guess--
} else if input == "h" {
guess++
} else if input == "y" {
fmt.Println("Yay! I got it in", tries, "tries!")
} else {
fmt.Println("Not sure what that means..")
}
if guess < 0 || guess > 99 {
fmt.Println("Sorry, that's out of my range!")
break
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.