'Why am i not getting the print message?

in this little number guessing game, I have made in python, I intend on giving an output saying: Good job! You guessed correct. But whenever you guess the correct answer it doesn't show the output as I want it to. Am I making a silly error or is it something else? following is the code:

 import random
range_1 = int(input("Enter your first range: "))
range_2 = int(input("Enter your second range: "))
x = random.randint(range_1, range_2)
print("Number chosen!")
guess = int(input(f"Guess the answer between {range_1} and {range_2}: "))

while guess != x:
    if guess > x:
        guess = int(input("Go lower: "))

    elif guess < x:
        guess = int(input("Go higher: "))
    
    else:
        print("Good job! You guessed correct!")
    


Solution 1:[1]

The problem is that the "you guessed correct" statement is in the while loop. Once guess has the correct value, the loop is exited and the print statement is never reached.

One fix is to put this print statement after the loop. Consider the following:

while guess != x:
    if guess > x:
        guess = int(input("Go lower: "))

    elif guess < x:
        guess = int(input("Go higher: "))

print("Good job! You guessed correct!")

Solution 2:[2]

Fine job mate :) Just put it after the loop, because when condition is met: guess == x it will not stay in the loop to get it in elif.

while guess != x:
    if guess > x:
        guess = int(input("Go lower: "))

    elif guess < x:
        guess = int(input("Go higher: "))

print("Good job! You guessed correct!")

If you want it inside the loop badly, then you might go with:

while guess != x:
    if guess > x:
        guess = int(input("Go lower: "))

    elif guess < x:
        guess = int(input("Go higher: "))

    if guess == x:
        print("Good job! You guessed correct!")

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 Ben Grossmann
Solution 2 NixonSparrow