Questions about type Python3

Asked

Viewed 39 times

0

I was wondering if you could use the type thus:

media = input("Digite o numero: ")
média = input("Digite outro numero: ")

if media == type('str'):
    print("oi")

else:
    print("Tchau")

That is, to check whether the variable media is str, and if it is, print a message.

If it’s not, then it goes on.

I wanted to use this for a calculator. In case it puts something other than int or float, say you can’t put characters in such a place.

1 answer

1

You can, just put the type on the other side.

media = 'exemplo'

if type(media) == str:
    print('oi')
else:
    print('tchau')
  • is right - but the most common is to use the call isinstance. Would be: if isinstance(media, str):

Browser other questions tagged

You are not signed in. Login or sign up in order to post.