0

I am creating a function that appends a list with inputs. There needs to be exactly 24 items for hours in a day.I am using the range function to do this. I need to validate this input. However I can't seem to get it to validate properly every time or i've gotten proper validation but the code is prompting more than 24 inputs.

def get(list):
for i in range(24):
    tem=float(input("Hourly tempature (00:00-23:00): "))
    if tem < -50 or tem > 150:
        print ("Enter tempatures between -50 and 130")
    else:
        tem=float(input("Hourly tempature (00:00-23:00)"))


    list.append(tem)

2 Answers2

2

Putting the input in the else block, not the if, means your code prompts for input again within the loop when the first input is correct, instead of incorrect.

In any case if they enter something wrong it won't check again. You need to use a while loop. See https://stackoverflow.com/a/23294659/2482744

Community
  • 1
  • 1
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

Several points:

  1. You might want to consider telling the user which hour they are setting the temperature for: e.g. input("Temperature at "+str(i)+":00 hours:")
  2. You should clarify whether you want the temperature to be less than or equal to 150, or less than or equal to 130, since at the moment the text given to the user suggests that the temperature has to be less than or equal to 130, but your if statement suggests that it has to be less than or equal to 150.
  3. You probably shouldn't be using the built-in list as a variable. Try using a variable that describes its purpose, like myTemperatureList
  4. Your code currently prompts for input again when the input is within the temperature bounds, but prints an error message (not getting an extra input) when the temperature is out of bounds. This means that when the temperature input is within the bounds, the user will be prompted for input twice, and the second input will be added to your list. However, when the temperature input is outwith the bounds, although an error message will be printed, the user will not be prompted for a second input, and the temperature outwith the bounds will be added to your list.

Expanding on point 3 above, what you want the input validation code to do is prompt for a temperature, and check if that temperature is within the bounds. If it is, then the value should be added to your list, if it isn't, then the value should be discarded, and the user should be prompted for input again.

This can be done in several ways. For instance, using a while loop, a possible solution might be this:

def get(myTemperatureList):
    for i in range(24):
        while True:
            #This is so that when an error message is printed,
            #the user is prompted again for input, for as long as they
            #are providing bad input
            try:
                #You'll want this try-except block in case the user doesn't enter a number
                tem=float(input("Temperature at "+str(i)+":00 hours:"))
                #This is basically the sames as in your code

                if tem < -50 or tem > 150:

                    print ("Enter a temperature between -50 and 130")
                    #Same logic as in your code, prints an error message
                    #when the temperature is out of bounds
                else:
                    #If the temperature is valid, break out of the while loop

                    break
            except ValueError:
                print("Enter a number")
        myTemperatureList.append(tem)

You could also solve this problem in other ways, for instance, using recursion with a validation function.

penalosa
  • 170
  • 1
  • 13
  • Thank you for this. There are many concepts I do not yet know and this helped not only the current problem but also a host of others. – zjmccauley Oct 23 '16 at 21:43
  • I am finishing up now, however it appears the list was only being appended with the last input of the 24 inputs. – zjmccauley Oct 23 '16 at 21:57
  • @zjmccauley My bad, it seems SO stripped the indent when I pasted the code in. I've fixed it now. – penalosa Oct 23 '16 at 22:08
  • I hadn't known that the indentation of the append statement did matter. So in this case, it is necessary that it be indented in-line with the for statement for it to append each iteration of the range()? – zjmccauley Oct 23 '16 at 22:18
  • @zjmccauley The append statement should be at the same indentation level as the while statement (one indentation level more than the while loop), so that it is executed once on every pass through the for loop. – penalosa Oct 23 '16 at 22:25