我正在尝试根据潮汐高度制作时间表生成器,并且我在下面的列表中有潮汐高度,每个值代表特定时间潮汐的高度,例如上午9点= 111点10点~123等。
tide_list = [111, 123, 135, 143, 146, 140, 128, 113, 99]
我想仅在潮汐高于100时才显示消息,例如在这种情况下 9:00〜16:00 除了设置多个if语句之外,还有什么好的方法吗?
我尝试将列表的每个值放入单个变量中,但看起来太乱了。
更新:基本上试图简化以下代码
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...
答案 0 :(得分:0)
您只需要一个for
循环和一个if
语句:
tide_list = [111, 123, 135, 143, 146, 140, 128, 113, 99]
for height in tide_list:
if height > 100:
print(height)
答案 1 :(得分:0)
这个答案假定在给定时期内潮流不会多次出现。
# Sample data
tide_list = [111, 123, 135, 143, 146, 140, 128, 113, 99]
# Filter out the low values and replace the high values with their index in the list (plus an offset)
above = [i+9 for i, level in enumerate(tide_list) if level >= 100]
# You could replace this with the error logic of your choice
assert len(above) >= 1, 'The tide never came'
# Format and print the result
print('{0:02d}:00~{1:02d}:00'.format(above[0], above[-1]))
答案 2 :(得分:-1)
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:
start ='9:00~'
elif time10>=100:
start ='10:00~'
elif time11>=100:
start="11:00~"
elif time12>=100:
start ='12:00~'
elif time13>=100:
start="13:00~"
elif time14>=100:
start ='14:00~'
elif time15>=100:
start="15:00~"
elif time16>=100:
start ='16:00~'
elif time17>=100:
start="17:00~"
else:
print("NO START TIME WAS SET!! CHECK DATA")
if time17>=100:
end ='17:00'
elif time16>=100:
end ='16:00'
elif time15>=100:
end="15:00"
elif rime14>=100:
start ='14:00'
elif time13>=100:
end="13:00"
elif time12>=100:
end ='12:00'
elif time11>=100:
end="11:00"
elif time10>=100:
end='10:00'
elif time19>=100:
end="9:00"
else:
print('NO END TIME SET CHECK DATA')
print start+end