-1

I am trying to make a loop which will continue to ask for inputs unless a number out side of 0-100 is entered.

posting homework description as well to avoid getting shit on again. In this assignment, you will design and implement a Python program that has three while loops.

The last set of readings is for humidity values(integers) between 0 and 100 (both values included).Any value outside this range is a sentinel value. The program computes the latest of the humidity readings. There need not be any readings at all because the first line itself may be outside the range 0 through 100. Your code should not fail even if there are no humidity readings.

while humid in range(0,100):

this is what I have right now, and it stops after one number.

amanton
  • 1
  • 4
  • 1
    In Python you don't need to instantiate variable type with 'humid = int()'. Remove this line and add 'humid=0' before the while condition. – Tony Pellerin Oct 23 '18 at 16:43
  • ...but note that, while unnecessary, both `humid=int()` and `humid=0` should have the same behavior (setting the `humid` variable to integer 0). – larsks Oct 23 '18 at 16:45
  • If you Google the phrase "Python input integer", you’ll find tutorials that can explain it much better than we can in an answer here. Also, you should learn to use a `for` loop -- that's the functionality you're trying to implement. – Prune Oct 23 '18 at 16:46
  • i have humid=0 at the front end. not sure how to post my whole code in a comment without it looking cancerous, other wise I would. my home works states I need to use while loops, otherwise I would try other things. – amanton Oct 23 '18 at 16:48
  • i am not having issues with inputs and integers. It takes the inputs, the problem is it only does it once. – amanton Oct 23 '18 at 17:05

1 Answers1

1

input() returns a string value, which will never be in the numeric range 0-100, so your loop exits immediately.

As a first step to fixing your code, you can immediately convert the result of input() by wrapping it in a call to int(), like this:

humid = int(input('Enter humidity: '))
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • while temp != "": temp = int() temp_num += 1 temp_total += temp temp = input('Enter temperature: ') this code works though with the input. – amanton Oct 23 '18 at 16:45
  • `temp = int()` does not do what you think it does. – John Gordon Oct 23 '18 at 16:47
  • well without it it wasnt allowing me to press enter to cancel the loop. it works with it in there. using int(input()) wasnt working either. – amanton Oct 23 '18 at 16:53