Golang : Number guessing game with user input verification example
Helping a Golang student from Mexico. She is building a simple random number guessing game and she wants to know how to check the user input to determine if the user input is indeed an integer value.
Simple, use the strconv.ParseInt()
to parse the user input and if the error != nil, then we know the user input is not integer
For example,
userNumber, err := strconv.ParseInt(userInput, 10, 0)
if err != nil {
fmt.Println("You must enter an integer value.")
}
The full program. Here you go!
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
)
var (
userInput string
attempts int = 0
)
// https://www.socketloop.com/tutorials/golang-random-integer-with-rand-seed-within-a-given-range
func numRandom(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min
}
func main() {
numberToGuess := numRandom(1, 10)
//fmt.Println(numberToGuess)
for {
attempts++
fmt.Println("A number is generated between 1 to 10. What do you think the number is: ")
// see https://www.socketloop.com/tutorials/golang-fix-fmt-scanf-on-windows-will-scan-input-twice-problem
// for scanf under Windows OS
fmt.Scanf("%v\n", &userInput)
// check if userinput is integer
userNumber, err := strconv.ParseInt(userInput, 10, 0)
if err != nil {
fmt.Println("You must enter an integer value.")
} else if numberToGuess == int(userNumber) {
break
} else if int(userNumber) < numberToGuess {
fmt.Printf("Smaller than the number. Try again\n")
} else {
fmt.Printf("Larger than the number. Try again\n")
}
}
fmt.Printf("Yup, the number is %v. You guessed it correctly after %v attempts\n", numberToGuess, attempts)
}
Sample output:
A number is generated between 1 to 10. What do you think the number is:
2
Larger than the number. Try again
A number is generated between 1 to 10. What do you think the number is:
1
Yup, the number is 1. You guessed it correctly after 2 attempts
Happy coding!
References:
https://www.socketloop.com/tutorials/golang-fix-fmt-scanf-on-windows-will-scan-input-twice-problem
https://www.socketloop.com/tutorials/golang-random-integer-with-rand-seed-within-a-given-range
See also : Golang : Fix fmt.Scanf() on Windows will scan input twice problem
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.4k Golang : Join lines with certain suffix symbol example
+13.1k Golang : Count number of runes in string
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+7.1k Golang : How to convert strange string to JSON with json.MarshalIndent
+7.9k Golang : Randomize letters from a string example
+5.9k Golang : Process non-XML/JSON formatted ASCII text file example
+9.5k Golang : Resumable upload to Google Drive(RESTful) example
+9.8k Golang : Find and replace data in all files recursively
+8.6k Golang : Gaussian blur on image and camera video feed examples
+26.8k Golang : Find files by name - cross platform example
+6.9k Golang : Gorrila mux.Vars() function example
+9.5k Golang : Turn string or text file into slice example