1

I'm trying to compare a input that the user receives that checks if the input is actually a number. Here's what I have so far:

numstring = input("Choose a number between 1 and 10")

and then to compare I have it in a method like this:

def check(a):
   if int(a) == int:
       print("It's a number!")
   else:
       print("It's not a number!")

It always prints out that it's not a number, even though it is.

Goal of this program: I'm making a guessing game:

import random

numstring = input("Choose a number between 1 and 10")

def check(a):
    if int(a) == int:
        print("It's a number!")
    else:
        print("It's not a number!")

    if abs(int(a)) < 1:
        print("You chose a number less than one!")
    elif abs(int(a)) > 10:
        print("You chose a number more than ten!")
    else:
        randomnum = random.randint(1, 10)
        randomnumstring = str(abs(randomnum))
        if randomnum == abs(int(a)):
            print("You won! You chose " + a + "and the computer chose " + randomnumstring)
        else:
            print("You lost! You chose " + a + " and the computer chose " + randomnumstring)


check(numstring)

Thanks for any help! The actual code works, but it just fails at checking it.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Some Guy
  • 351
  • 1
  • 4
  • 20

3 Answers3

10

You can just use str.isdigit() method:

def check(a):
    if a.isdigit(): 
        print("It's a number!")
    else:
        print("It's not a number!")

The above approach would fail for negative number input. '-5'.isdigit() gives False. So, an alternative solution is to use try-except:

try:
    val = int(a)
    print("It's a number!")
except ValueError:
    print("It's not a number!")
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
5

int(a) and int are not equal because int() returns an integer and just int with no () is a type in python2.(or class in python3)

>>> a = '10'
>>> int(a)
10
>>> int
<type 'int'>

If you're only dealing with integers then use try-except with int as others have shown. To deal with any type of number you can use ast.literal_eval with try-except:

>>> from ast import literal_eval
>>> from numbers import Number
def check(a):
    try:
        return isinstance(literal_eval(a), Number)
    except ValueError:
        return False

>>> check('foo')
False
>>> check('-100')
True
>>> check('1.001')
True
>>> check('1+1j')
True
>>> check('1e100')
True
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
4

Try to cast an input to int in try/except block:

numstring = input("Choose a number between 1 and 10")

try:
    a = int(numstring)
    print("It's a number!")
except ValueError:
    print("It's not a number!")

If you are using python 2, consider switching to raw_input() instead of input(). input() accepts any python expression and this is bad and unsafe, like eval(). raw_input() just reads the input as a string:

numstring = raw_input("Choose a number between 1 and 10")

try:
    a = int(numstring)
    print("It's a number!")
except ValueError:
    print("It's not a number!")

Note that raw_input() has been renamed to input() in python 3.

Also see:

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195