Solved

Verifying type in input

Posted on 2004-10-13
3
258 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 125 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

803 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