-3
import random


def validate_user_input(input):
    try:
        val = int(input)
    except ValueError:
        print("Please enter a valid input.")
        return True


hidden_number = random.randint(1, 10)

user_input = ""

while user_input != hidden_number:
    user_input = input("Guess the number from (1 to 1000).")
    if validate_user_input(user_input) is True:
        continue
    else:
        if int(user_input) == hidden_number:
            print("You have guessed the correct number.", hidden_number)
        elif int(user_input) > hidden_number:
            print("You have guessed a number higher than the correct number.")
        elif int(user_input) < hidden_number:
            print("You have guessed a number lower than the correct number.")
        else:
            print("You have guessed the correct number.")

When the user has inputted the correct number I want the while function to terminate but it instead continues to loop. I tried setting a variable as true in the else function instead but that doesn't work either.

Python 3.5+

1 Answers1

0

The problem is that the input function returns a string, and not an integer. The right way to make your code work without changing it would be to declare user_input as an integer somewhere in the loop. One way to do it to keep the validate_user_input function active and useful would be to put the int() directly in the while declaration, just like this:

import random


def validate_user_input(input):
    try:
        val = int(input)
    except ValueError:
        print("Please enter a valid input.")
        return True


hidden_number = random.randint(1, 10)

user_input = 0

while int(user_input) != hidden_number:
    user_input = input("Guess the number from (1 to 1000)."))
    if validate_user_input(user_input) is True:
        continue
    else:
        if int(user_input) == hidden_number:
            print("You have guessed the correct number.", hidden_number)
        elif int(user_input) > hidden_number:
            print("You have guessed a number higher than the correct number.")
        elif int(user_input) < hidden_number:
            print("You have guessed a number lower than the correct number.")
        else:
            print("You have guessed the correct number.")
  • Modifying the user input changes nothing. When the user guesses the correct number I want the program to terminate instead of asking the user to input a number again. – M.A.Coding Jul 14 '21 at 16:07
  • Modifying the user input into an integer makes the while loop understand that the user_input and the hidden_number are the same, so it will terminate the loop, and so the program, because your while is only active when the two variables aren't the same, in that case, a string and an integer. – Mathieu Bigneault Jul 14 '21 at 16:11
  • I changed what you told me to change and it did nothing. The while function still keeps repeating the question ("Guess the number from (1 to 1000).") even after I guess the correct number. – M.A.Coding Jul 14 '21 at 16:14
  • For me, transforming the input into an INT breaks the loop instantly. Can you show your code? – Mathieu Bigneault Jul 14 '21 at 16:15
  • My apologies, you are right I just made a mistake in reading your solution. It works fine now thank you. +1 – M.A.Coding Jul 14 '21 at 16:21
  • No problem, have a nice coding session. – Mathieu Bigneault Jul 14 '21 at 16:22
  • The reason I originally removed the int() was so that I could prevent the program from crashing when someone enters a value that is not an integer. When I added the Int() back now it breaks the validate_user_function. – M.A.Coding Jul 14 '21 at 16:23
  • In that case, you could transform the user_input into an int directly on the line after the call of the validate_user_input function, or better: you could just change your while loop to be: while int(user_input) != hidden_number: Like that, you could still use your validate_user_input function. – Mathieu Bigneault Jul 14 '21 at 16:25
  • I changed my loop and I also reset the user_input to 0 after the validate_user_input function and It worked. – M.A.Coding Jul 14 '21 at 16:48