1

I'm working on an assignment for my Computer Science class and the assignment asked for us to get an input and determine whether the input is a valid integer and whether the input is a float (2 questions).

I've got most of it down, but the only part that is messing with me is when the input is alphanumeric (i.e. 123dfkj). I tried using

while not num.isdigit(): 

but a problem comes up when the user input is negative.

3
  • 4
    Why not simply attempt to cast the input string to int and catch the exception? EX: int(input("input an integer")) with throw a ValueError for any non-integer value. The same would work for a float. Unless you're explicitly not allowed to use Python's built in facilities for doing this. Commented Feb 24, 2015 at 23:37
  • For the purposes of your assignment, do you consider an input of 3.0 to represent an integer? Commented Feb 25, 2015 at 3:36
  • No, 3.0 would be considered a float :T Commented Feb 25, 2015 at 19:04

4 Answers 4

2

This is relatively straight forward by casting the input and using try/except blocks to catch an exceptions.

val = input() try: int(val) except ValueError: print("Not an integer") try: float(val) except ValueError: print("Not a float") 
Sign up to request clarification or add additional context in comments.

1 Comment

As I said to the other answerer, simply casting to int may not answer this question, since any floating point can be cast to int, but it is not true that any floating point is "a valid integer".
1

Easiest way is to follow the EAFP principle and cast the input to an integer and catch the exception if it isn't.

EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

try: myint = int(myinput) except ValueError: # myinput was not an integer 

3 Comments

Be careful simply casting to integer. 3.5 can successfully be cast to int but it is not a valid integer.
Whilst you are correct that 3.5 is not a valid integer, the int() method will remove anything after the decimal place, leaving you with a valid integer. In your example, the end product is 3. I agree however, this may not be ideal depending on the situation.
@Karl True, but the requirements might state that non-integer input should be rejected rather than truncated. In that case, you'd have to cast the input to both int and float and compare the results.
0

Another solution would be using regular expression.

float: ^-?[1-9]{1}[0-9]*.{1}[0-9]*$

integer: ^-?[1-9]{1}[0-9]*$

However, this does not consider using "e" as an exponent (e.g. 6.022e23)

Comments

0

Be careful with these other answers recommending simply to cast to int, since any floating point value can be truncated to an integer successfully.

You may need to check that the floating point representation of the value is equal to its integer representation, so that, e.g., 3 and 3.0 will count as an integer but not 3.5.

>>> def is_it_a_number(value): ... try: ... float(value) ... print('It can be represented as floating point!') ... except ValueError: ... print('It cannot be represented as floating point.') ... else: ... try: ... if float(value) == int(float(value)): ... print('It is a valid integer.') ... except ValueError: ... print('It is not a valid integer.') ... >>> is_it_a_number(3) It can be represented as floating point! It is a valid integer. >>> is_it_a_number(-3) It can be represented as floating point! It is a valid integer. >>> is_it_a_number(3.0) It can be represented as floating point! It is a valid integer. >>> is_it_a_number(3.5) It can be represented as floating point! >>> is_it_a_number('3.0') It can be represented as floating point! It is a valid integer. >>> is_it_a_number('3.5') It can be represented as floating point! >>> is_it_a_number('sandwich') It cannot be represented as floating point. 

4 Comments

The input is a string - notice that the question refers to isdigit(). Your code in fact claims that "3.0" is not a valid integer (which may or may not be what OP wants, but isn't what your examples imply will happen with the input that would actually be passed to your function). As an aside, this means that your criticisms of the other answers are mistaken.
Oh, I see. I copied the wrong version over. Should be float(v) == int(float(v)). Regardless, my point stands and you can see that the implementation I ended up with does indeed treat 3.0 as a valid integer. There was just a mistake in what I copied over into the answer.
Additionally, since every other answer also employs straight integer casting, they would also fail for '3.0' as you point out.
I also now notice that I am the only one who considered the phrase "take an input" did not necessarily mean invoke the built-in input function and take input from the command line. This answer only applies if you are trying to write a function that will take some unspecified type of input (i.e., duck typing).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.