Calculating the stops between stations.
★★★The Victoria line is a London Underground line that runs between Brixton in south London and Walthamstow Central in the north-east, via the West End. It is printed in light blue on the Tube map and is one of the only two lines on the network to run completely underground. There are sixteen stations on the line. This program outputs the number of stops between two stations on a ticket on the Victoria line.
Write a program to output how many stops there are between two stations on the Victoria line.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_station that:calculate_stops that:get_station is called to input the name of the first station on the ticket.get_station is called again to input the name of the second station on the ticket. Note the stations can be input in any order, not just the order shown in the list of stations.calculate_stops is called to calculate the number of stops.Enter station: Stockwell
Enter station: Pimlico
Stockwell to Pimlico is 2 stops.
Enter station: Brixton
Enter station: Stockwell
Brixton to Stockwell is 1 stop.
Enter station: Green Park
Enter station: Vauxhall
Green Park to Vauxhall is 3 stops.
# London Underground program
# -------------------------
# Subprograms
# -------------------------
# Function to input a valid station name
def get_station():
---
valid_station = False
---
# Validation
while not valid_station:
---
station_input = input("Enter station: ")
---
# Lookup check
if station_input in stations:
---
valid_station = True
---
else:
---
print("Invalid station.")
return station_input
---
# Function to calculate the number of stops between two stations
def calculate_stops(ticket):
---
start_at = stations.index(ticket[0])
stop_at = stations.index(ticket[1])
---
stops = abs(start_at - stop_at)
---
return stops
---
# -------------------------
# Main program
# -------------------------
# Victoria line
stations = ["Brixton", "Stockwell", "Vauxhall", "Pimlico", "Victoria", "Green Park", "Oxford Circus", "Warren Street", "Euston", "King's Cross", "Highbury & Islington", "Finsbury Park", "Seven Sisters", "Tottenham Hale", "Blackhorse Road", "Walthamstow Central"]
---
ticket = [0, 0] # The two stations on the ticket
---
ticket[0] = get_station()
ticket[1] = get_station()
---
stops = calculate_stops(ticket)
---
if stops == 1:
---
print(ticket[0], "to", ticket[1], "is", stops, "stop.")
---
else:
---
print(ticket[0], "to", ticket[1], "is", stops, "stops.")