Is there a way to check from a users input what type it is? and do something like so:
if input == str:
do this
elif input == int:
do this
else:
do this
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
isinstanceto check the type of something — But you really probably don’t want to do this …This also depends a lot on how you’re getting the input from the user. If you’re using
raw_input, then it is always a string and you’ll need totryto convert it to whatever type you want:One basic principle of python is duck-typing. If an object looks like a duck, sounds like a duck and smells like a duck, then it’s a duck (provided you only care what the object looks, smells and sounds like). Ultimately, in python it’s best to
trysomething to see if it is a “duck”. If it isn’t a duck, then you catch the Exception and handle it (if you know how). This is the primary reason you don’t see people usingisinstancevery much in their code and the reason I warn against using it.