Several points:
- 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:")
- 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.
- You probably shouldn't be using the built-in
list
as a variable. Try using a variable that describes its purpose, like myTemperatureList
- 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.