0

My current code:

while True:
    surjuv = input("What is the juvenile survival rate?")
    if surjuv.isinstance(float)==True and float(surjuv)<=1 and float(surjuv)>=0:
        break

It needs to make sure that surjuv is a float, but the following error comes up when 0.5 is entered:

Traceback (most recent call last):
File "python", line 81, in <module>
File "python", line 21, in main_menu
File "python", line 51, in enter_gen0
AttributeError: 'str' object has no attribute 'isinstance'
Michael H.
  • 3,323
  • 2
  • 23
  • 31
Sir_Steve
  • 11
  • 1
  • 2
    Possible duplicate of [How do I check if a string is a number (float) in Python?](http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float-in-python) – Chris_Rands May 02 '17 at 09:38

1 Answers1

5

The syntax for isinstance would be isinstance(surjuv, float), but this will always fail in your case because Python 3 input returns a string.

Convert the input to a float, and when that fails do whatever error handling is necessary.

try:
    surjuv = float(input('...'))
except ValueError:
    continue # or whatever you want
timgeb
  • 76,762
  • 20
  • 123
  • 145