-1

The user is supposed to enter a number from 1 to 10 and the code is supposed to check for 3 conditions if the input is not a number ask for entering again, if the input is out of 1-10 ask for entering again, and if the number is in range of 1-10 break the loop and store the value in the variable. the first two checks are running correctly, but the last is not working, the loop does not break, and it says invalid input like other conditions. What is the problem?

 while True:
        num_guess = input("How many times you want to guess [1-10]: ") # Asking for number of guess
        if num_guess != int: # Checking for non number input
            print("Invalid input")
        elif int(num_guess) < 1 or int(num_guess) > 10: # Checking for out of range input
            print("Invalid input")
        elif 1 <= int(num_guess) <= 10: # if input is in the range just break the loop and store the answer
            break

6 Answers6

3

if num_guess != int should be:

try:
    num_guess = int(num_guess)
except ValueError:
    print("Not a number!")
    continue

Reference: How to check if string input is a number?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
2

The input() method takes you input and converts it into a string input. So even if you enter number 12, the input will converted to “12” and stored in num_guess. So, the best way to check if the input is a number or not, is using the isdigit() method.

So the line 3 of your code should be written as

if not num_guess.isdigit():
Sören
  • 1,803
  • 2
  • 16
  • 23
keidakida
  • 713
  • 4
  • 12
  • "By default"? So there is a way to make input() return something other than a string? – Sören Oct 29 '22 at 22:06
  • If you look at the return type of `input()` it returns a string, so it is up to you to type cast it into int or float or anything like `int(input())` or `float(input())` – keidakida Oct 29 '22 at 22:08
  • Yeah, I know that. The why do you claim that `input()` only returns a string "by default"? – Sören Oct 30 '22 at 06:26
  • https://docs.python.org/3/library/functions.html#input By the text “by default” I meant it only returns a string. – keidakida Oct 30 '22 at 19:42
0

Use if not num_guess.isnumeric() to check if all the characters of the string are numeric characters. num_guess is always a String, so answers suggestion to check the type are wrong.

bitflip
  • 3,436
  • 1
  • 3
  • 22
0

Reason why it keeps saying "Invalid input" it's because num_guess is always a string.

You can try it this way, I hope this helps.

# store input result
result: int

while True:
    num_guess = input(">> How many times you want to guess [1-10]: ") # Asking for number of guess

    try:
        int(num_guess) # raise ValueError if num_guess is NaN
    except ValueError:
        print(">> Interger 1 ... 10 required.")

    if int(num_guess) < 1 or int(num_guess) > 10: # Checking for out of range input
        print(">> Interger from 1 to 10 required.")
    elif 1 <= int(num_guess) <= 10: # if input is in the range just break the loop and store the answer
        # store the value in the result variable
        result = int(num_guess)
        print(">> Saving...")
        break
Mane Motha
  • 11
  • 4
  • There's no sense repeatedly calling `int(num_guess)`. Call it once and save the result if no exception is raise. (And put less code in the `try` statement; the only thing that belongs there is the attempt to call `int`, not the following `if` statement.) – chepner Oct 30 '22 at 21:38
-1

Replace if num_guess != int with if type(num_guess) != int. This is because you need to check what data type the number is, using an equality operator with int would try to check if the object is the class int.

In python everything is an object (or a data type), even the constructors for objects.

user17301834
  • 443
  • 1
  • 8
-1

If you want to check if input is a number you must get type of the variable using type(YOUR_VARIABLE) so python would check the type of it returning values like: "int", "str", "float" and by that way you can check if variable is an int!

or just don't read it and steal this code:

 while True:
        num_guess = input("How many times you want to guess [1-10]: ") # Asking for number of guess
        if type(num_guess) != int: # Checking for non number input
            print("Invalid input")
        elif int(num_guess) < 1 or int(num_guess) > 10: # Checking for out of range input
            print("Invalid input")
        elif 1 <= int(num_guess) <= 10: # if input is in the range just break the loop and store the answer
            break

but im still not 100% sure so you can check it and correct me if I did something wrong