The Boredless Tourist

https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-tourism/modules/cspath-boredless-tourist/projects/the-boredless-tourist

Hello,

can you help me with the exercises 11 and 12? This is my code so far:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "Sao Paulo, Brazil", "Cairo, Egypt"]
test_traveler = ["Erin Wilkes", "Shanghai, China",["historical site", "art"]]
def get_destination_index(destination):
  destination_index = destination.index()
  return destination_index
print(get_destination_index("Los Angeles, USA"))

In bash it gives me TypeError, saying “index ( ) takes at least 1 argument (0 given)”. What exactly should I do?

Thank you.

Edit:

This is the best I could do but still doesn’t work well, instead of index 2 it gives me 0:

def get_destination_index(destinations):
  destination_index = destinations.index(str(destinations))
  return destination_index
print(get_destination_index("Los Angeles, USA"))

Here I changed the name of the function’s parameter from destination to destinations, although the project itself tells me to name it destination. I did it so because "Los Angeles, USA’’ makes part of the already defined variable of destinations, and not destination.

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "Sao Paulo, Brazil", "Cairo, Egypt"]
test_traveler = ["Erin Wilkes", "Shanghai, China",["historical site", "art"]]
def get_destination_index(destination):
  destination_index = destination.index()
  return destination_index
print(get_destination_index("Los Angeles, USA"))

Let’s work with your first code. You almost had it. When you call your function, you send the parameter,"Los Angeles, USA". Inside the function you declare the variable destination_index and need to assign a value. The value you are looking for is in your destinations array. You want the index of the value you passed which is stored in the destination variable. Should look like this:
destination_index = destinations.index(destination)

2 Likes

Is there any real purpose in having the str() constructor deployed?

1 Like

Thank you very much for your explanation sir, now it works!

I would have one more question if you don’t mind: could you explain how is the variable destination connected to the string “Los Angeles, USA”, I’m still a bit confused about it? If instead of destination we had destinations I would understand because “Los Angeles, USA” originally makes part of destinations, but obviously there’s something I’m missing in not seeing the connection between that string and the variable destination? Are they connected in this code line:

destination_index = destinations.index(destination)

In sense, destination being between parenthesis of destinations (i. e. its index function) automatically means it is connected to the elements of destinations?

Sir, first I ran the code line without string function but then I was given an error, something like that the element from the list can’t be converted to a string implicitly. That’s the reason I tried adding string function.

print(get_destination_index("Los Angeles, USA"))
def get_destination_index(destination):

If you’ll notice the structure of these two statements, they are very similar. In your definition of the get_destination_index(destination) function statement, get_destination_index is the function name and the (destination) is the name of a variable that is accepting the value passed to the function when you call it. (destination) could be anything you choose.
In the (print(get_destination_index("Los Angeles, USA")) statement, you are calling the function, and passing the part in ()'s as the parameter for the function to use. In this case that parameter is "Los Angeles, USA". Essentially what is happening with the passed parameter is this: destination = "Los Angeles, USA" If you renamed destination to something else like simply x, the code would still work as such:

def get_destination_index(x):
  destination_index = destinations.index(x)
  return destination_index
print(get_destination_index("Los Angeles, USA"))

Hopefully this helps. Happy coding!

1 Like

The destinations data is a list of strings. When we call the function with a string argument, it should not raise any errors…

x = destinations.index(destination)

Notice that the index method belongs to the list, not the string.

That error was not in the argument being string data, but in the wrong object being accessed by the index() method. Adding the str constructor did not cure the problem, did it?

Trace the data to where it is used and that is where you will find your problem. Don’t make assumptions or take a stab if you are just guessing. Look for the problem.

1 Like