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 :
you can`t reach the else of the print.
The while
loop check if guess is different than x, and the 2 if check if bigger or lower.
fix:
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: "))
print("Good job! You guessed correct!")