I'm a beginner trying to learn Python and decided to try an exercise where I would take an input, and distinguish its type; whether its an integer, floating point or a string.
As I understand it, Python treats all inputs as strings, so even when I enter a number/decimal point, the input is considered a string. I have overcome the first step in differentiating whether the input is a string or otherwise via this:
def get():
prompt = 'Give a number or a word.\n'
x = input(prompt)
try:
float(x)
print('You entered a number.')
except:
print('You entered a string.')
I take advantage of 'try' and 'except', as float(x) will return an error if x is a string and not a number/floating point. Next, I deduce that I can use a similar method to distinguish between integers and floating point numbers, but I don't know what can cause an error for an integer that wouldn't for a floating point, vice versa. (since float(x) where x is an integer works fine)
Is there such a thing so that I can use a similar method to distinguish between decimal number inputs and integers?
print (type(1.),type(1))infis a float without one and then there is a whole class of representation by scientific notation (3e-1==0.3) which doesn't contain any either. Not to speak of all the flavours ofnan.x.index('.')will "cause an error for an integer that wouldn't for a floating point", andtypeshows Python agrees with that definition.