7 Practice
In this section, we’ll practice applying our understanding of for
loops and if else
conditional statements to unfamiliar code.
The code example below may look intimidating! But remember, we’re less concerned here about understanding every detail about what the code is doing, and more about using what we’ve learned about programming logic as a lens through which to begin interpreting code.
7.1 Nested for
loops and if else
statements (in Python)
One way to contextualize unfamiliar code is to break down a big chunk of code into self-contained sections. For instance, if the code is doing a lot of things - looping through some task, with some conditions - we want to understand the order of operations of each thing that is being done.
Below is an example of code that is scraping content from a set of website pages and saving that content locally. This code chunk uses nested for
loops, and nested if else
statements with the for
loops and within other if else
statements.
Exercise: Review the code chunk below and identify on which line each for
loop and each if else
statement starts and ends.
- First
for
loop- Starts on line:
- Ends on line:
- Second
for
loop- Starts on line:
- Ends on line:
- First
if else
statement- Starts on line:
- Ends on line:
- Second
if else
statement- Starts on line:
- Ends on line:
- Third
if else
statement- Starts on line:
- Ends on line:
- Fourth
if else
statement- Starts on line:
- Ends on line:
# Code to scrape data from website
1. base_url = "https://nces.ed.gov/ipeds/datacenter/"
2. data_url = "DataFiles.aspx?"
3. year_base = "year="
4. years = ["1995", "1996", "1997", "1998"]
5. session_id = "&sid=4f8f293f-df75-42cd-9cc0-ed184270cf17&rtid=7"
6.
7. # what type of files do you want to save?
8. file_type = ["zip","csv"]
9.
10. # where do you want to save locally?
11.
12. # save_loc is redundant, but a reminder that you should have locally folders that reflect the years,
13. # because that's where below is saving to
14. save_loc = years
15.
16. # what kind of prefix do you want on your files
17. save_prefix = "ipeds_"
18.
19. # scrape
20. for year in years:
21. url = base_url+data_url+year_base+year+session_id
22. webpage = requests.get(url)
23. soup = BeautifulSoup(webpage.content, 'html.parser')
24.
25.
26. if os.path.isdir("./"+year):
27. # notice output path has "year" as a variable indicating folders
28. output_path = "./"+year+"/"+save_prefix+year+"_"+".html"
29.
30. if os.path.exists(output_path):
31. print(output_path+" already exists. Did NOT save.")
32. else:
33. # saving the html file
34. print("saving "+output_path)
35.
36. with open(output_path, 'wb') as file:
37. file.write(webpage.content)
38.
39. # saving files linked on original html page which meet file type requirement
40. for link in soup.find_all('a')[0:16]:
41. data_target = link.get('href')
42. # data_target[-3:]
43. if any(extension == data_target[-3:] for extension in file_type):
44. wget_url = base_url+data_target
45. wget_save = "./"+year+"/"+save_prefix+data_target.replace("/","-")
46.
47. if os.path.exists(wget_save):
48. print(wget_save+" already exists. Did NOT save.")
49. else:
50. print("SAVING "+ wget_url+ " at local location: "+wget_save)
51. wget.download(wget_url,wget_save)
52. time.sleep(.25) # be kind, don't look like a DDOS attack
53. else:
54. print("\n!!! save directory "+year+" does NOT exist. please create\n")
Start and end locations
-
First loop
- Starts on line: 20
- Ends on line: 54 (encompasses entire code block line 20 until end of line 54)
-
Second loop
- Starts on line: 40
- Ends on line: 52 (encompasses entire code block line 40 until end of line 52)
You may have noticed another loop on line 43. If so, great job! That one originally snuck by the instructors when putting together this answer key. We’re leaving it as is though, as a reminder that even more experienced programmers miss things and make mistakes. So if you didn’t catch this additional loop, you’re in good company!
-
First
if else
statement- Starts on line: 26
- Ends on line: 54 (the matching
else
is on line 53, with end of action on line 54)
-
Second
if else
statement- Starts on line: 30
- Ends on line: 37 (matching
else
is on line 32, with end of action on line 37)
-
Third
if else
statement- Starts on line: 43
- Ends on line: 45 (no matching
else
, ends on line 45 and goes into nextif
)
-
Fourth
if else
statement- Starts on line: 47
- Ends on line: 52 (matching
else
is on line 49, with end of action on line 52)