'In Go, how do stay in a for loop, but print a different output if user guesses too high or too low

I'm trying to develop a guessing game in GO that gives the user three attempts to guess the random number correctly.

It mostly seems to work, I'm using a for loop to count the amount of lives left. If I guess the number correctly, it prints what I want:

"You guessed it!"

But the issue is, say for example, the random number is 5. If I guessed 4, it would print

"Too low"

which is correct, but in the second attempt if I was to guess 6, it would still say

"Too low"

Alternatively, if the random number was 5, and on the first attempt I guessed 6, it would print

"Too high"

If I was to guess 4 on the second attempt, it would still say

"Too high"

I think I'm getting caught in the for loop somewhere, but I don't know how to exit it without resetting the attempts count back to 3.

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    secret := getRandomNumber()
    fmt.Println(secret)

    fmt.Println("Guess the number")

    _guess := guessRandomNumber()

    for i := 3; i > 0; i-- {

        if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i, "guesses left.")
        } else if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        } else if _guess > secret {
            fmt.Println("Too high")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        }

        if _guess == secret {
            fmt.Println("You guessed it!")
            i = 0
        } else if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        } else if _guess > secret {
            fmt.Println("Too high")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        }
    }

}

func getRandomNumber() int {

    rand.Seed(time.Now().UnixNano())
    return rand.Int() % 11
}

func guessRandomNumber() int {
    var guess int
    //fmt.Println("Guess again:")
    fmt.Scanf("%d", &guess)
    return guess
}

Any help would be greatly appreciated. I'm really liking GO so far.



Solution 1:[1]

I ended up using a for loop with a switch statement to solve the issue.

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
secret := getRandomNumber()
fmt.Println(secret)

fmt.Println("Guess the number")

_guess := guessRandomNumber()

guessCorrect := false
guessCount := 3

for guessCorrect == false && guessCount != 0 {

    switch {
    case _guess < secret:
        fmt.Println("Too low,", guessCount, "guesses left.")
        fmt.Scanf("%d", &_guess)
        guessCount--
        continue
    case _guess > secret:
        fmt.Println("Too high", guessCount, "guesses left.")
        fmt.Scanf("%d", &_guess)
        guessCount--
        continue
    default:
        fmt.Println("You got it!")
        guessCorrect = true
    }

}
}

func getRandomNumber() int {
    rand.Seed(time.Now().UnixNano())
    return rand.Int() % 11
}

func guessRandomNumber() int {
    var guess int
    fmt.Scanf("%d", &guess)
    return guess
}

Solution 2:[2]

Deploying multi-region high available web application in Azure with App Services

  • Deploy your application into one or more secondary regions and load-balance your HTTP traffics to those regions based on availability.
  • If your application up and running in the primary region, traffic goes to the primary region. If your primary region not available, traffic goes to the secondary regions.
  • You can use the priority routing method for this. There are few azure load balancers which provide priority load balancing options. Azure Traffic Manager, Azure Application Gateway, and Azure Front Door.

Creating Resource Groups

  • Create azure resource Group (each region one resource group) in two or more different Azure regions.

Creating web apps in Azure App Services Firstly, create and deploy the app to the primary region enter image description here

  • Using the same way, create a web app in the secondary region as well. enter image description here

Deploying web apps to Azure App Service

  • Before you publish the solution, change the AppRegion value of the appsettings.json, located in the root folder of the solution. This will help you to identify the web application that responds to your request.
  • Do the same thing for the secondary region app service as well. Before you publish the application into the secondary region, make sure that you have changed the AppRegion property of the appsettings.json to “Secondary”.

Please refer Deploying multi-region high available web application in Azure with App Services and Highly available multi-region web application for more information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GetOutOfThatGarden-
Solution 2 HarshithaVeeramalla-MT