Hi guys,
I have a list with 6 items in it. I need to output how many items there are between 2 of the list items the users asks for.
How can I do this?
So for example this is my code:
Code bus_stops= ['New York', 'Los Angeles', 'New Jersey ', 'Boston', 'Miami', 'Mexico']
start_station = input("What is your start destination")
end_station = input("What is your end destination")
totalstations = #STUCK HERE
I need to calculate how many bus stops there are between the user submitted start and end stations in the bus_stop list.
I've read that this is possible with the numPY library but I have't found a way to do it yet.
Code bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']
start_station = input("What is your start destination")
end_station = input("What is your end destination")
start_pos = 0
end_pos = 0
for i in range(len(bus_stops)):
if start_station == bus_stops[i]:
start_pos = i + 1
break
for i in range(len(bus_stops)):
if end_station == bus_stops[i]:
end_pos = i
break
totalstations = end_pos - start_pos
print totalstations
Loop until you have position of x, same with y.
y - x = d
Pretty self explanatory.
(Sep 6, 2015 02:09 PM)cobaltscraper Wrote: Code bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']
start_station = input("What is your start destination")
end_station = input("What is your end destination")
start_pos = 0
end_pos = 0
for i in range(len(bus_stops)):
if start_station == bus_stops[i]:
start_pos = i + 1
break
for i in range(len(bus_stops)):
if end_station == bus_stops[i]:
end_pos = i
break
totalstations = end_pos - start_pos
print totalstations
Thank you! that worked perfectly, I completely forgot about looping 😖
DO NOT DEAL WITH THIS MEMBER
PHP Code
<?php bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico'] start_station = 'New York' end_station = 'Boston' print (bus_stops.index(end_station)-1) - bus_stops.index(start_station)
:)
Umm... There is no need for a loop here.
PHP Code
<?php bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']
start_station = input("What is your start destination") end_station = input("What is your end destination")
total = max(bus_stops.index(start_station), bus_stops.index(end_station)) - min(bus_stops.index(start_station), bus_stops.index(end_station))
(Sep 7, 2015 09:46 AM)Introvert Wrote: Umm... There is no need for a loop here.
PHP Code
<?php bus_stops= ['New York', 'Los Angeles', 'New Jersey', 'Boston', 'Miami', 'Mexico']
start_station = input("What is your start destination") end_station = input("What is your end destination")
total = max(bus_stops.index(start_station), bus_stops.index(end_station)) - min(bus_stops.index(start_station), bus_stops.index(end_station))
Thanks for this, it does make it simpler. 🤝
|