Quick Python code question

Soldato
Joined
12 Feb 2009
Posts
4,359
Location
London
Im doing a Python course and im having trouble trying to understand what I have done wrong.

When an input of 4.7 which is in the range is entered it outputs 4.7.

Code:
# initialise the input_list with the given values
temperatures = (4.7)
# initialise the output_list to the empty list
faulty_fridge = []
# for each input_value of the input_list:
for temperature in temperatures:
    # if the input_value satisfies the condition:
    if temperature not in range(0,6):
        # append the input_value to the output_list
        faulty_fridge = faulty_fridge + [temperature]
# print the output_list
print(faulty_fridge)


My understanding is that the input is only added to the output list if it meets the 'if' criteria which it does not? Im sure there should be another way to do it rather than use a range but im a little stuck.

Something like this makes sense to me but Im getting a syntex error around the second condition of the 'if' so is obviously wrong.

Code:
if temperature <= 0 or => 5:
 
Last edited:
Soldato
OP
Joined
12 Feb 2009
Posts
4,359
Location
London
Never mind figured it out.

Code:
# initialise the input_list with the given values
temperatures = [-0.1, 0, 0.1, 1, 4, 4.6, 6, 7]
# initialise the output_list to the empty list
faulty_fridge = []
# for each input_value of the input_list:
for temperature in temperatures:
    # if the input_value satisfies the condition:
    if temperature < 0 or temperature > 5:
        # append the input_value to the output_list
        faulty_fridge = faulty_fridge + [temperature]
# print the output_list
print(faulty_fridge)
 
Back
Top Bottom