Guessing Game

An infinite loop that keeps asking until the user guesses correctly.

package main

import "fmt"

func main() {
	secret := 7

	fmt.Println("Guess the number (1-10):")

	for {
		var guess int
		fmt.Print("> ")
		fmt.Scan(&guess)

		if guess == secret {
			fmt.Println("Correct!")
			break
		} else if guess < secret {
			fmt.Println("Too low. Try again.")
		} else {
			fmt.Println("Too high. Try again.")
		}
	}
}

💻 Run locally

Copy the code above and run it on your machine

© 2026 ByteLearn.dev. Free courses for developers. · Privacy