Jappyman Jappyman -4 years ago 70
Python Question

python list if statement

I am trying to make a timetable generator based on the tide heightand I have the tide height in a list like below, each value represent the height of the tide at specific time e.g 9am=111 10am=123 etc..

tide_list = [111, 123, 135, 143, 146, 140, 128, 113, 99]


I want to display a message only if tide is above 100, eg in this case
9:00~16:00
is there any good way to do this other than setting up multiple if statements?

I tried putting the each value of the list into single variables but it looks too messy.

update:basically trying to simplify the following code

tide_list = [111, 123, 135, 143, 146, 140, 128, 113, 99]

time9,time10,time11,time12,time13,time14,time15,time16,time17=tide_list

if(time9>=100 and time10>=100 and time11>=100 and time12>=100 and
time13>=100 and time14>=100 and time15>=100 and time16>=100 and
time17>=100):
print('9:00~17:00')
elif time9>=100 and time10>=100 and time11>=100 and time12>=100 and
time13>=100 and time14>=100 and time15>=100 and time16>=100 and time17<100:
print('9:00~16:00')
elif time9>=100 and time10>=100 and time11>=100 and time12>=100 and
time13>=100
and time14>=100 and time15>=100 and time16<100 and time17<100:
print('9:00~15:00')
elif time9>=100 and time10>=100 and time11>=100 and time12>=100 and
time13>=100 and time14>=100 and time15<100 and time16<100 and time17<100:
print('9:00~14:00')
etc...

Answer Source

All you need is a for loop and a single if statement:

tide_list = [111, 123, 135, 143, 146, 140, 128, 113, 99]

for height in tide_list:
    if height > 100:
        print(height)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download