Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare variable types in python? [duplicate]

Tags:

python

types

I'm working on a simple program that will ask for the weather and temperature and output what clothing the user should wear. However, I've gotten to the point where I want to make sure the user can't enter "g" degrees or any other string. Is there a simple way to compare variable types? In other words, is there something along the lines of:

if (type(temp) == 'str'):

    print("Invalid. Try again.")

Or something similar that isn't too complicated? Personally, I'm fine with using advanced functions and whatnot, but that would look sketchy to my CS teacher.

like image 274
William Adams Avatar asked Jan 08 '23 09:01

William Adams


1 Answers

You pretty much have it right, just no need for the quotes.

>>> type(5) == int
True
>>> type('5') == int
False
>>> type('5') == str
True
like image 94
user4020527 Avatar answered Jan 09 '23 23:01

user4020527