-1

I am trying to create a program that stores a user input between 1 and 0.

It works fine when I only input numbers, however, the program fails when anything else is inputted.

I tried adding a third clause into the while statement but that made the whole program fail.

I suspect float(input) is the erroneous code -- When I don't have that, the sums later on in the program fail, suggesting that the variable is just a string.

What's wrong here?

#  Juvenile survival rate must be between 0 and 1 #
juvenile_survival = 3
while juvenile_survival < 0 or juvenile_survival > 1 or juvenile_survival.isdigit() == False:
    juvenile_survival = float(input("What is your juvenile survival rate? Remember this should be between 0 and 1"))
print("Your juvenile survival rate is ", juvenile_survival)
armatita
  • 12,825
  • 8
  • 48
  • 49
Ollie
  • 107
  • 1
  • 1
  • 9
  • 1
    You are declaring `juvenile_survival = 3` in the first line. `isdigit` is defined only for `str`ings. See [How can I read inputs as integers in Python?](http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python) if you want to accept numbers from `input`. – Bhargav Rao Jun 04 '16 at 13:20
  • Thanks for your help. My friend said to declare juvenile_survival as something that the loop won't accept in order to get the loop going in the first place. I've read the link too, but I'm still confused as what to do, as the post talks predominately about integers, but I need floats. Could you post the correct code please? Thanks – Ollie Jun 04 '16 at 14:33
  • I'm not sure what you are trying to accomplish, why are you making a `float` of the `input`? –  Jun 04 '16 at 14:40

1 Answers1

0

I'm not sure what you are trying to accomplish:

Do you want something like:

juvenile_survival = 3
while juvenile_survival < 0 or juvenile_survival > 1:
    try:
        juvenile_survival = int(input("What is your juvenile survival rate? Remember this should be between 0 and 1: "))
    except ValueError as ex:
        print(ex)
print("Your juvenile survival rate is ", juvenile_survival)
  • I changed your int(input bit to float (as the number will probably be 0.6 or something) and now it works. Thanks! – Ollie Jun 04 '16 at 15:29