Beginner needs help with first code, guessing game

I only started a couple of days ago to try to learn programming, first with python and now I decided to try Kotlin instead. As an excercise I’m trying to make a simple guessing game just to learn the basics. I’ve watched some Youtube videos and Googled around and tried to piece together bits from here and there.

So far the code below is working as intended, except for the guessCount variable. My intent is to increase it by 1 for every guess the user makes but it seems to be stuck at 0. What am I doing wrong?

import java.util.*

const val rightAnswer = 45
var guessCount = 0
const val maxGuesses = 3
var remainingGuesses = (maxGuesses-guessCount)

fun main(){
println(“Welcome to The Guessing Game!”)
while (true){
println(“You have $remainingGuesses guesses left.\nGuess a number:”)
val guessReader = Scanner(
var guess = guessReader.nextInt()
println(“You guessed: $guess”)
if(guess != rightAnswer && remainingGuesses > 0){
println(“Guess was wrong.”)
else if(remainingGuesses < 0){
println(“You’re out of guesses.”)
else {
println(“You guessed the right number, congratulations!”)


remainingGuesses is never reassigned after you define it.
I suspect that’s the issue.

Thanks! it works after putting it inside the main function instead of at the top.