1

For the simple snippet below

a = raw_input("Enter a number: ")

How can I write an exception such that if a string is entered, the user is re-prompted to try again and again until an integer is entered.

In other words, keep doing this until an integer is entered.:

if a.isdigit == False:
    raw_input("Try again: ") 
2
  • my answer is no use for you i will delete it, if you want just positive numbers choose one of the answer that use isdigit() , hope i did help this time :) Commented Jan 20, 2011 at 2:15
  • I think my question was poorly worded that confused everyone -_-. Thanks for your help anyway! Commented Jan 20, 2011 at 2:19

4 Answers 4

2

The usual way to achieve this is to use int() with exception handling:

>>> n = None
>>> while n is None:
...    a = raw_input("Enter a number: ")
...    try:
...      n = int(a)
...    except ValueError:
...      print "Not a number."
...
Enter a number: abc
Not a number.
Enter a number: cauliflowers are my favourite vegetable
Not a number.
Enter a number: 12
>>>

However if you would prefer to avoid exception handling you can take the following approach. Note that it does not accept negative integers, but only the natural numbers 0, 1, 2, ..., N:

>>> while True:
...   a = raw_input("Enter a number: ")
...   if a.isdigit():
...     break
...   print "Not a number."
...
Enter a number: I like beetroot too
Not a number.
Enter a number: -500
Not a number.
Enter a number: 500
>>>

It becomes a little more involved to handle negative integers in this manner, requiring three cases, one for empty strings, one for negative integers and one for natural numbers:

>>> while True:
...   a = raw_input("Enter a number: ")
...   if len(a) > 0 and ((a[0] == '-' and a[1:].isdigit()) or a.isdigit()):
...     break
...   print "Not a number."
...
Enter a number: Celery stinks.
Not a number.
Enter a number:
Not a number.
Enter a number: -
Not a number.
Enter a number: 1
>>>
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a good reason to use try...except? If an addition int(a) > 0 condition was added to the answers you downvoted, wont it make their solution valid as well?
@Nai In answer to your first question, the try... except is required because int(a) returns an integer, not a boolean, and uses exceptions to signal the case where it is dealing with a string that is not a valid integer. This is why adding int(a) > 0 to the isdigit answers is not enough, as int will throw an exception if confronted with a non-integer representing string.
@Nai Having said that, if you do only want to find natural numbers (0, 1, 2, ..., N) rather than integers (-N, ..., -2, -1, 0, 1, 2, ..., N) I would recommend updating your question and using Jeff or Michaels answers below.
1

You can use a while loop:

a = ""
while not a.isdigit():
    a = raw_input("Enter a number: ")

If you want an error message it becomes something like:

a = raw_input("Enter a number: ")
while not a.isdigit():
    a = raw_input("Try again: ")

3 Comments

@fmark I think it's implied that he means natural numbers; he used .isdigit right in his question...
Sorry, I can't change my downvote now unless you edit your answer.
@fmark Don't worry about it, unless you badly want your 1 rep back (you can always edit the answer too; it doesn't need to be the poster)
1
while True:
    a = raw_input("Enter a number:")
    try:
        i = int(a)
        break
    except ValueError:
        print "Not a number"

Comments

0
a = raw_input("Enter a number: ")
while (not a.isdigit()):
    a = raw_input("Try again: ")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.