Reputation: 1
I currently have this how can I get it so it checks for a float and uses a while loop
def get_float():
number = input('Input a decimal number ')
while number != float:
print('bad input ')
number = input('Input a decimal number ')
else:
return number
get_float()
right now even if I enter a decimal number it says bad input and asks for another input
Upvotes: 0
Views: 209
Reputation: 1
figured it out:
def get_float():
while True:
number = input('Input a decimal number ')
try:
if "." in number:
floatnumber = float(number)
print(floatnumber)
break
else:
print("invalid number")
except ValueError as err:
print(err)
get_float()
Upvotes: 0
Reputation: 33351
number = input('Input a decimal number ')
while number != float:
This is wrong for a few reasons.
First, input()
returns a string, so number
is a string. It is not a floating point value.
Second, even if number
were a proper floating point value, it is not equal to float
, because float
is a type object.
It seems like you really meant if type(number) != float
. (But again, that would always be false here, because number
is a string.)
Upvotes: 0
Reputation: 70377
Sometimes it's better to ask for forgiveness than permission.
def get_float():
while True:
number = input('Input a number ')
try:
return float(number)
except ValueError:
print('\n bad input\n ')
Upvotes: 3