2

I've written the following code in Python 3.x to validate the user's input:

while True:
    try:
        answer = int(input("Enter an integer: "))
    except ValueError:
        print("That's not a whole number. Try again.")

I know that inputting 'hi' or 'hi46' would be strings (and would cause ValueError).

What data type would inputting ' ' (nothing) be? What about inputting ']%$' (symbols)?

3 Answers 3

3

Assuming you're using python 3.X, everything the user inputs will be a string. Even numbery looking things like "23" or "0". int(thing) doesn't validate that thing is of the integer type. It attempts to convert thing from whatever type it is now, into the integer type, raising a ValueError if it's impossible.

Demonstration:

>>> while True:
...     x = input("Enter something: ")
...     print("You entered {}".format(x))
...     print("That object's type is: {}".format(type(x)))
...
Enter something: hi
You entered hi
That object's type is: <class 'str'>
Enter something: hi46
You entered hi46
That object's type is: <class 'str'>
Enter something: 
You entered
That object's type is: <class 'str'>
Enter something: ]%$
You entered ]%$
That object's type is: <class 'str'>
Enter something: 23
You entered 23
That object's type is: <class 'str'>
Enter something: 42
You entered 42
That object's type is: <class 'str'>
Enter something: 0
You entered 0
That object's type is: <class 'str'>
Sign up to request clarification or add additional context in comments.

3 Comments

I also corrected my question to clarify that I'm using Python 3.x.
So can I refer to something like 7.93 as 'a floating point which is considered as/entered as a string'?
Not really. Saying "<thing> is a floating-point number" in a programming context typically implies "<thing>'s type is float", which isn't true for "7.93". The most honest statement would be ' "7.93" is a string which float() can successfully parse'
1

You can do this without relying on an exception using isdigit():

answer = input("Enter an integer: ")
while not answer.isdigit():
    print("That's not a whole number. Try again.")
    answer = input("Enter an integer: ")
answer = int(answer)

isdigit() tests to see if the input string is made up entirely of numbers that can be converted with int().

3 Comments

Is this more or less efficient?
You would have to time them to tell, don't forget to add a break into the original to exit the while loop though if you do test them. I've heard that a try/except is a little bit slower than an if, but in this case we've got to call isdigit(). It does follow "Flat is better than nested" for a Pythonic plus. But it also follows the "Look before you leap" philosophy, whereas "Better to ask forgiveness" is considered more Pythonic, which is how the original was written.
I timed them using iPythons %timeit and a random.choice in place of an input. The original was coming in at 19.6us, whereas this one was coming in at 10.6us. When I disassembled them to bytecode this one was 25 instructions long compared to the originals 29 instructions. So I guess this one is a little bit more efficient.
0

String, everything that you input() will be a string. And all of them will raise value error except for C-c if int() raises it.

2 Comments

Sorry, could you please clarify what C-c is?
Ctrl-c = key board interrupt or any other key combination that indicates End of Input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.