python - Making sure input is a float -
this might noob question, me this? need make sure input strictly float. current code returns error, no matter type of input.
pricepercandy = float(input('please enter price per candy (2 decimal places): ')) if pricepercandy.isalpha() or pricepercandy.isalnum(): print("please enter price 2 decimal places.") pricepercandy = float(input('please enter price per candy (2 decimal places): '))
you can write simple function read input until float value enterd user
>>> def readfloat(): ... while true: ... try: ... value = float(input("enter value ")) ... return value ... except valueerror: ... print "please enter price 2 decimal places"
test
>>> readfloat() enter value "asdf" please enter price 2 decimal places enter value "qwer" please enter price 2 decimal places enter value 1.2 1.2
Comments
Post a Comment