In other programming languages you have a do-while statement for this task, i.e. you can first ask the user for a number and then execute the code until the correct number is given by the user. In Python there is no such statement. But that's okay, you have several other possibilities to make the code work.
Common to all these possibilities (except variant 2!) is that you already need an initialization of a value to check the condition in the while loop. For your example (user can obviously enter number between 1 and 50), it would be sufficient to simply choose 0, e.g. guessing_games = 0. A bit more robust is to take a number that the user cannot enter at all, e.g. infinity: import math; guessing_games = math.inf.
Now all conditions are given to use a while loop. As the name actually says, it is about executing the same block over and over again. To prevent the program from running infinitely, we need a termination condition. This is clear in your case: The execution of the block should stop when the user has guessed the right number.
This means that the block should run within the while loop until the condition for the loop is no longer fulfilled: while guessing_games != guess_number. Of course it is important that the variable guessing_games can be changed inside the block. This is the case because the user is always asked to enter a new number at the beginning of the block execution.
Variant 1: Using a simple termination condition and printing success message after while loop
import math
guess_number = 33
guessing_games = math.inf
while guessing_games != guess_number:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
if guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
print("Great job! You got the number correct!")
Variant 2: Using an explicit termination condition within the block with the break statement
guess_number = 33
while True:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
if guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
else:
print("Great job! You got the number correct!")
break
Variant 2 shows you a possibility to avoid the initialization of guessing_games using while True (Otherwise as seen in variant 1 and 3, the initialization have to be there to avoid the error: NameError: name 'guessing_games' is not defined). But then you have to be careful that the loop will certainly end at some point.
Variant 3: Very similar to variant 1, except that the success message can be seen as part of the while loop.
import math
guess_number = 33
guessing_games = math.inf
while guessing_games != guess_number:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
if guessing_games > guess_number:
print("Too high! Guess again.")
elif guessing_games < guess_number:
print("Too low! Guess again.")
else:
print("Great job! You got the number correct!")
See also the Python documentation:
The while statement is used for repeated execution as long as an expression is true:
while_stmt ::= "while" assignment_expression ":" suite
["else" ":" suite]
This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.
A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.
The last thing I would actually recommend is to check whether the user has entered a number at all:
try:
guessing_games = int(input("Try to guess the number I am thinking of, 1-50: "))
except ValueError:
print("Only numbers are allowed.")
continue