hmm, little question here from a slightly new user.

David Fisher python at rose164.wuh.wustl.edu
Thu Mar 23 19:37:19 EST 2000


----- Original Message -----
From: "Falknor" <falknor at worldspy.net>
To: <python-list at python.org>
Sent: Thursday, March 23, 2000 5:29 PM
Subject: hmm, little question here from a slightly new user.


> How can I check to see if a value is an int type? ie something like
>
> int = raw_input("Enter your int value.")
>
> if is_int(int):
>     floater = int(int)
>

You can test type with the built-in isinstance():

import types
if isinstance(my_var,types.IntType):
    print "it's an int!"
elif isinstance(my_var,types.StringType):
    print "it's a string!"

However, raw_input() will return a string, not an int.  So you'll need to
coerce it to an int first:

my_string = '1234'
my_int = int(my_string)

This will throw a ValueError exception if it can't make an int out of the
string.  So it sounds more like you want to catch the exception instead of
testing for the type.  I see somebody else has posted how to catch the
exception, so no point in repeating it.

Good Luck,
David





More information about the Python-list mailing list