2

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.

3
  • @AshwiniChaudhary. He is using print() function. So, I guess 3. Commented Aug 18, 2013 at 19:26
  • @RohitJain print() works in py2 as well, but prints tuples. Commented Aug 18, 2013 at 19:27
  • @AshwiniChaudhary. Yeah. Thanks for the info. I know that :) Commented Aug 18, 2013 at 19:29

3 Answers 3

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!") 
Sign up to request clarification or add additional context in comments.

Comments

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 

1 Comment

isinstance(int("a"), int) will throw exception. And '-1'.isdigit() will return False.
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.

6 Comments

May be OP is using py3.
@AshwiniChaudhary yeah, thanks, I've added a link to the relevant thread.
Your first approach will fail. isinstance("5", int) will return False. And isinstance(int("a"), int) will throw exception.
@RohitJain sure, it's fragile. But if the OP is using python 2 and the input is 5 - it'll work.
@RohitJain "5" is not of int type - I understand. But, I mean that input() will return 5 (int), not "5" - it evaluates the input, reads it as an expression in python 2.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.