# Validating for only numbers in python one after another(in sequence)
a = raw_input()
#if a is a number then it should take next raw_input i.e., 'b' else it should ask for 'a' again
b = raw_input()
#if b is a number then it should take next raw_input i.e., 'c' else it should ask for 'b' again
c = raw_input()
#if c is a number then it should take next raw_input i.e., 'd' else it should ask for 'c' again
## This should be done upto 'e'
Asked
Active
Viewed 117 times
0

Haifeng Zhang
- 30,077
- 19
- 81
- 125

Annas Lee
- 13
- 2
2 Answers
1
use a.isdigit()
to validate string a
is digit or not
str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise.

Haifeng Zhang
- 30,077
- 19
- 81
- 125
-
what about if it is a float value – Annas Lee Apr 21 '16 at 18:42
-
@AnnasLee good catch. If your number contains dot(.) or slash(/) and so on, you can remove those characters and check. for example `"1.1".replace(".", "").isdigit()` – Haifeng Zhang Apr 21 '16 at 18:46
0
Make use of if..else
construct for the conditional checks and refer this post for the integer check.
Edit 1
I have not executed it,but you could try the following and see if it works-
def ask(text):
temp = raw_input(text)
try:
retVal = int(temp)
return retVal
except ValueError:
return False
a = False
b = False
c = False
while not a:
a = ask("Enter A:")
if a == False:
continue
while not b:
b = ask("ENter B:")
if b == False:
continue
while not c:
c = ask("Enter C:")

Community
- 1
- 1

Mayur Buragohain
- 1,566
- 1
- 22
- 42
-
Hi, Mayur Buragohain. Can you please code it from 'a' to 'e' raw_input. I am unable to do it! – Annas Lee Apr 21 '16 at 18:32
-