Reputation: 13
I'm currently working on a small assignment for school.
I need to print the current station where the train is at the moment and the stations that are left from the list.
I have used a nested for
loop, but I can't get the inner for
loop to work properly. Do I need to create a variable with +=1
?
My code:
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])
My output:
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
Upvotes: 1
Views: 130
Reputation: 9599
You're almost done, you just need to increment begin
on each iteration.
train_station=['Amsterdam-Central','Amsterdam-Amstel','Utrecht']
begin = 0
for x in train_station:
print("Current station is: "+x)
print("Stations to go: ")
begin += 1
for y in range(begin, 3, 1):
print(train_station[y])
And a more pythonic solution:
for i, x in enumerate(train_station, 1):
print("Current station is: "+x)
print("Stations to go: ")
for y in train_station[i:]:
print(y)
Upvotes: 2