Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - how to test if a users input is a decimal number [duplicate]

I am writing a program that needs to check if a users input is a decimal (the users input must be a deciamal number),I was wondering how I could test a varible to see if it contains only a decimal number.

Thanks, Jarvey

like image 811
hpj Avatar asked Dec 10 '22 13:12

hpj


1 Answers

U also could use a Try/Except to check if the variable is an integer:

try:    
    val = int(userInput) 
except ValueError:    
    print("That's not an int!")

or a float:

try:    
    val = float(userInput) 
except ValueError:    
    print("That's not an float!")
like image 52
Stan Vanhoorn Avatar answered May 12 '23 16:05

Stan Vanhoorn