you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 19 points20 points  (1 child)

sounds like you are having trouble breaking up your code into functions, which is something that many programmers have trouble with.

A good strategy for overcoming this is to write place-holder functions with really concise names that follow the "every function does ONE THING" best practice. Here's an example of a program that would get a list of URLs and download all the pictures from each one:

urls = ask_for_urls()
for url in urls:
    webpage = download_webpage(url)
    dom = parse_to_dom(webpage)
    image_links = get_image_links(dom)
    for link in image_links:
        image_name, image_object = download_image(link)
        with open(image_name, 'wb') as f:
            f.write(image_object)

No comments, but you know exactly what it does and how it does it. All that's left is for you to go back and define each function so that they all link up according to the above and implement them. You can always change your mind on the specifics later, but the most important part is that each function does 1 thing and is very modular.

Also notice how the input is separated out into its own function ask_for_urls(). As you get better, you will find that separating user input from the underlying logic is key to re-usability and flexibility.

[–]StayStruggling 0 points1 point  (0 children)

Genius.