Python Forum
Help needed with lists please - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help needed with lists please (/thread-10856.html)



Help needed with lists please - mattick - Jun-10-2018

Hi I'm fairly new to Python, been doing some work with lists, but have got stuck. Hoping someone maybe can help.

I need to take an input list of numbers. Than print the numbers that are not in the range of 0 to 5. So any numbers that are not 0,1,2,3,4,5 will be printed as my output.


Here is what I've trailed so far, with no luck.

temperatures = [12, 4.5, -1, -3.4, 5, 6, 2]                
    
wrong_temps = []

a = 0
b = 5

for i in temperature:
    if i < a or i > b:
    wrong_temps.append(i)

print(wrong_temps)



RE: Help needed with lists please - Larz60+ - Jun-10-2018

you need to indent after if:
temperatures = [12, 4.5, -1, -3.4, 5, 6, 2]                
     
wrong_temps = []
 
a = 0
b = 5
 
for i in temperature:
    if i < a or i > b:
        wrong_temps.append(i)
 
print(wrong_temps)



RE: Help needed with lists please - buran - Jun-10-2018

you have it more or less right, there is error (note that the list is temperatures, temperature)
and also the indentation error on 10 (which may be because of missing python tags which I fix for you)

temperatures = [12, 4.5, -1, -3.4, 5, 6, 2]                
     
wrong_temps = []
 
a = 0
b = 5
 
for i in temperatures:
    if i < a or i > b:
        wrong_temps.append(i)
 
print(wrong_temps)
Output:
[12, -1, -3.4, 6]



RE: Help needed with lists please - mattick - Jun-10-2018

I have to solution, many thanks for your help.


RE: Help needed with lists please - volcano63 - Jun-10-2018

You may also do (with more Pythonic names)
if not low <= temp <= high: