Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Verifying type in input

Posted on 2004-10-13
3
Medium Priority
?
284 Views
Last Modified: 2010-04-16
I would like to be able to verify the type of input from an input statement, but I can't seem to be able to figure it out.  For example:

num=raw_input("Enter a number: ")

I would like to then verify that num is an int.

I am sure that it is a bit more easy than in java, and that is probably my problem...I am looking to far into it and can't see the forest through the trees.

Thanks for your help,
Brian
0
Comment
Question by:bnblazer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12305582
num = None
while not num:
    try:
        num = int( raw_input( "Enter a number: " ) )
        print 'num is definitely an integer number'
    except:
        num = None

 
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 500 total points
ID: 12305593
basically, cast the result of raw_input() to int() (or float() if you want decimal places), catching exception in the case where the cast fails...

probably better to explicitly catch the correct exception, so other errors are not hidden...

num = None
while not num:
    try:
        num = int( raw_input( "Enter a number: " ) )
        print 'num is definitely an integer number'
    except ValueError:
        num = None
0
 
LVL 1

Author Comment

by:bnblazer
ID: 12306921
Thank you, that is exactly what I was looking for.

Brian
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question