Tip:
Highlight text to annotate it
X
[Dave] Now we have our get_next_target procedure,
and we know how to use it by using multiple assignment
to get the 2 results.
There's 1 serious problem with the get_next_target procedure that we have to fix
before we go on to the problem of outputting all of the links in the page.
The problem is we didn't really think very carefully about what should happen
if the input does not have another link.
Let's try and see what actually happens the way we've defined it.
Here's the code we have for get_next_target, and we're going to try an example.
First we'll try an example where there is a link,
so what we should get as the next target when this is the page
that's passed in is the link that's included,
which is between the double quotes inside the a href tag.
When we run that, we get our 2 outputs.
We got the string that's the link,
and we got 37, which is the position of the end quote.
And we can see it's printing out the 2 outputs as a tuple,
meaning the first one, the second one
surrounded by parentheses, and we could get those in an assignment like this.
Instead of just printing it, we'll use our double assignment,
getting those outputs, and we'll print the value of just the URL.
And we get the string http://udacity.com.
That worked well.
Now what happens if instead of passing in a page
that actually has a link, we pass in something that doesn't.
Let's say our page is just the text "good."
Now, there's no link to find,
but the code is going to run anyway, and what we see as the result
is we actually got the result "goo."
Probably not what we wanted,
and the reason we got the result "goo" is you remember the find command,
if it does not find what it's looking for, it returns -1.
And when we use -1 as an index, that means the last character of the string.
So what we end up with is getting all the characters except for the last character.
If we pass in something else that does have a double quote in it,
so now we'll pass in 'Not "good" at all!'
we see what we get back for the URL is Not.
This is not very useful.
This is pretty confusing behavior.
It's going to be very hard to tell when we've gotten to the last target
because maybe Not could be a valid URL, and we don't know that.
What we want to do is make get_next_target return something more useful
in the case where the input does not contain any link,
and the way we're going to do that we'll leave partly up to you.
But I'll give you a hint to get started.
We're going to want to change the code,
and you're going to want to put something into the code here.
If we found a link, well, we've got a value for start_link
that's the position of the start link.
If we didn't find a link, what we got back from find was -1,
and I want you to change what the get_next_target procedure does,
so when there's no link to find,
what we should return is the value None.
This is the special value that means there isn't anything.
And we'll return 0 as the endpos.
Let me write that out more precisely, but the hint is
you're going to have to change the code
after you've found the start_link to figure out
whether there is a link to find.
Your goal for this quiz is to modify the get_next_target procedure
that we've defined so that
if there is a link, it behaves exactly as it does now,
still returns the URL of the link target,
as well as the number of that position where the end quote is.
But if there's no link in the input string,
it should output None as the first output
and 0 as the second output, and None is a special value
that means it's not actually returning anything.