首页 > 解决方案 > 如何显示在这种情况下为 1 的第一站?

问题描述

现在火车在第一站,每个站都会停,除非到达第三站,不会停在那里。当我们到达最后一站时,我们会说再见。

我已经解决了这个问题,只是我未能在打印的同一语句中显示第一个站点。

current_station = 1
skip_station = 3
end_station = 7

# true if train isn't at the end station
while current_station < end_station:
    current_station = current_station + 1
    # if train reached  station # 3, skip that station
    if current_station == skip_station:
        continue
    else:
        print(current_station)
print('bye')

标签: pythonwhile-loopcontinue

解决方案


current_station = 1
skip_station = 3
end_station = 7
# Because now the train will be leaving
if current_station==1:
    print(f'Train left from station: {current_station}')
# true if train isn't at the end station
while current_station < end_station:
    current_station = current_station + 1
    # if train reached  station # 3, skip that station
    if current_station == skip_station:
        continue
    else:
        print(f"The Train is at station: {current_station}")

推荐阅读