1
我目前正在为学校做一个小型作业。嵌套循环中的跟踪计数器
我需要打印当前列车的位置,以及列表中剩下的车站。
我已经使用嵌套for
循环,但我不能让内部for
循环正常工作。我是否需要使用+=1
创建变量?
我的代码:
train_station=['Amsterdam-Central','Amsterdam-Amstel','Utrecht']
for x in train_station:
print("Current station is: "+x)
print("Stations to go: ")
begin=0
for y in range(begin,3,1):
print(train_stations[y])
我的输出:
Current station is: Amsterdam-Central
Stations to go:
Amsterdam-Central
Amsterdam-Amstel
Utrecht
Current station is: Amsterdam-Amstel
Stations to go:
Amsterdam-Central
Amsterdam-Amstel
Utrecht
Current station is: Utrecht
Stations to go:
Amsterdam-Central
Amsterdam-Amstel
Utrecht
提示:你'开始=在内部进行循环,这可能*不*你想要什么0'各一次。 – alfasin
它应该做什么? –
http://stackoverflow.com/questions/32548708/for-loop-how-to-take-one-off-every-time-you-pass-over/32548762#32548762 –