Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading