Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Verifying type in input

Posted on 2004-10-13
3
Medium Priority
?
289 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
  • 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

916 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