English subtitles

← Print All Links - Intro to Computer Science

Get Embed Code
9 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. So now we've modified our get_next_target procedure,
  2. and it will return None as the first output if there's no next target.
  3. Otherwise it will return the URL and the end_quote.
  4. And now all we have to do is figure out how to keep going.
  5. Let's look at what we were doing before.
  6. We turned these 4 lines into our procedure
  7. so now we can--instead of having all this code,
  8. we're going to call the get_next_target
  9. and assign the results.
  10. Now we've got the value of URL, which we do on a print out,
  11. so we're still going to print that out as before.
  12. And we still want to update the value of page,
  13. but we don't want to use end_quote now.
  14. What we want to use is the value that was returned here,
  15. which we assigned to the variable endpos.
  16. And then, well, we're doing it all again.
  17. So, all of the code here is just another call to get_next_target,
  18. and similarly, this would be endpos instead of end_quote.
  19. And we want to keep going until we get to the end,
  20. so how do we decide when we've gotten to the end?
  21. Well, we got to the end when the URL
  22. that's returned is None.
  23. And when the call to get_next_target returns None for the URL,
  24. we know we're done.
  25. We've seen a way to keep going. That's a while loop.
  26. We've seen a way to do a test.
  27. We want to test the URL.
  28. And we have everything we need now to print all the links on the page.
  29. I'm going to give you a start for how to write this procedure,
  30. and then you're going to finish it.
  31. What we're going to do is define a procedure
  32. that will print all the links on the page,
  33. and it takes the page as input, and so we want to use a while loop to keep going,
  34. and I am going to leave blank the test condition for the while loop.
  35. We'll leave that as something for you to figure out.
  36. In the body of the while loop, what we're going to call get_next_target,
  37. assigning URL and endpos to the result.
  38. This is just like what we were doing in the example code.
  39. Now what we need to do is check whether we got a valid URL,
  40. so what we were doing here,
  41. we assumed that we always got a valid URL,
  42. and we printed it out, and we kept going, but we need to do something to test
  43. whether the URL that we got back is None.
  44. That's what we'll do.
  45. We have if URL, and if that's true, that means we found a valid URL.
  46. We didn't get the value None as the result from get_next_target.
  47. And so we want to print that out as we were doing before.
  48. We'll print the URL, and we'll advance the page
  49. to the next position.
  50. If we didn't get a valid URL,
  51. that means that get_next_target did not find a link.
  52. There were no more links on the page,
  53. and we need to do something else, and I will leave blank
  54. what we need to do here.
  55. So, this is all we need for the code to print_all_links.
  56. There are 2 parts left for you to do as the quiz here.
  57. See if you can figure out what should go as the test condition for the while
  58. and in the block for the else.
  59. And if that's correct, you'll be able to print all the links on the page.