Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Comparison Solution

How to check if input is int and not string or float

number = input("Please enter a number: ")

number = int(number)

This is the 'right' code if you follow the video. But when the input is a string the code breaks. How can you check if the input is a int?

Rohan Tinna
Rohan Tinna
3,415 Points

You can use the type function in Python. The syntax is as follows: type(object) This function will return the type of the object passed in as argument (inside the brackets).

number = input("Enter a number:  ")
print(type(number))

# Now if we cast the number variable (now a string because input function returns a string) to an int and check the type again, we'll get a different result.

number = int(number)
print(type(number)) 

The output of the above python code (if you enter 3 as input) will be:

Enter a number: 3
<class 'str'>
<class 'int'>

This indicated that initially, the data type of number variable was a string (str) but when we casted it to an integer and printed out it data type again, it was changed to integer (int).

I hope this answer will help you out ;)

If you want, you can connect with me on these platforms:
Snapchat: https://www.snapchat.com/add/rohantinna
Twitter: https://www.twitter.com/rohantinna
Instagram: https://www.instagram.com/rohantinna or https://www.instagram.com/irohantinna
Email: rohantinna3@gmail.com
Treehouse: @rohantinna @rohant

3 Answers

Hi Marco,

You can use a try/except block for this kind of work:

try:
    number = input("Please enter a number: ")
    int(number)
except ValueError:
    print("That's not a number..")
else:
    print(number)

You basically tell Python to check if the number provided by the user is an int. If it is, then the try block passes and the code in the else block runs. In this case, we print out the number.

If it's not an int, then we raise a ValueError. Which prevents your program from crashing, and enables you to print a message to your user letting them know what's going on.

Hopefully, this answers your question! You can find more information about Try/Except in the Python Basics Course.

https://teamtreehouse.com/library/expecting-exceptions

if number.isdigit():
    number = int(number)
Abhinandan vig
Abhinandan vig
445 Points

instead of using int, USE FLOAT eg:

number = float(number) --> in this case you don't have to worry about if user enter a floating point no and for string you can simply ask user in the first place that : print("Please only enter a no or decimal no !") Thanks ! Hope this helps !

I think it's better to have a fool-proof solution. If you know an error might occur, you might as well catch it/handle it in the first place and take the burden away from the user. Imagine that you are using your OS and there is a message "oh btw, don't click here because you'll get an error", that wouldn't be very user friendly.