Skip to content

anonymous /guesser.go
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
guesser.go
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.