Introduction to Python
davidbpython.com
Projects, Session 5
PLEASE REMEMBER:
|
|||||||||||||||||||||||||||||||||||||||||||||
All requirements are detailed in the homework instructions document. |
|||||||||||||||||||||||||||||||||||||||||||||
NOTE ON OPENING FILES |
|||||||||||||||||||||||||||||||||||||||||||||
If the file you want to open is in the same directory as the script you're executing, use the filename alone:
fh = open('filename.txt')
|
|||||||||||||||||||||||||||||||||||||||||||||
If the file you want to open is in the parent directory from the script you're executing, use the filename with ../:
fh = open('../filename.txt')
|
|||||||||||||||||||||||||||||||||||||||||||||
If the file you want to open is in a child directory from the script you're executing, use the filename with the child directory name prepended:
fh = open('<childdir>/filename.txt')
|
|||||||||||||||||||||||||||||||||||||||||||||
(Replace <childdir> with the name of the child directory.) |
|||||||||||||||||||||||||||||||||||||||||||||
5.1 | Notes typing assignment. Please write out this week's transcription notes. The notes are displayed as an image named transcription in each week's project files folder. | ||||||||||||||||||||||||||||||||||||||||||||
This does not need to be in a Python program - you can use a simple text file. |
|||||||||||||||||||||||||||||||||||||||||||||
5.2 | Filepaths Exercises. | ||||||||||||||||||||||||||||||||||||||||||||
As usual, returned solutions will lose points. It is recommended to confirm (through testing) that your answer is correct before submitting to ensure that you will receive credit. Notations are not required for this solution. |
|||||||||||||||||||||||||||||||||||||||||||||
Start with the below file tree, which is available in this week's data folder:
dir1 ├── file1.txt ├── test1.py │ ├── dir2a │ ├── file2a.txt │ ├── test2a.py │ │ │ └── dir3a │ ├── file3a.txt │ ├── test3a.py │ │ │ └── dir4 │ ├── file4.txt │ └── test4.py └── dir2b ├── file2b.txt ├── test2b.py │ └── dir3b ├── file3b.txt └── test3b.py |
|||||||||||||||||||||||||||||||||||||||||||||
To complete this assignment, please open and edit each of the below 5 .py scripts in the tree so that they open the noted .txt files:
|
|||||||||||||||||||||||||||||||||||||||||||||
Your job is to fill in the relative filepath (i.e. not starting with C:\Users or /Users) needed to open the indicated file in the open(r'') function call in each script.
|
|||||||||||||||||||||||||||||||||||||||||||||
######## test2a.py: read file3a.txt ########
fh = open('') # add relative filepath here to open file3a.txt
######## test1.py: read file4.txt ########
fh = open('') # add relative filepath here to open file4.txt
######## test4.py: read file3a.txt ########
fh = open('') # add relative filepath here to open file3a.txt
######## test3a.py: read file1.txt ########
fh = open('') # add relative filepath here to open file1.txt
######## test2b.py: read file2a.txt ########
fh = open('') # add relative filepath here to open file2a.txt
# hint: you must go "down, then up" within the single filepath
|
|||||||||||||||||||||||||||||||||||||||||||||
See "Filepaths for Locating Files" slide deck from last session for a discussion to assist in completing this assignment. Send me any questions you may have. |
|||||||||||||||||||||||||||||||||||||||||||||
5.3 | Lookup Dictionary. Reading file states.csv (see the file in this session's source data), build a dict of pairs with each state's name as key and the abbreviation as the value (for example, New York as key and NY as value). Then, read user input for a state name. If the state name is a key in the dict, display the abbreviation for that state. If it is not a key in the dict (use 'if'), print the error message no state found with name "<keyname>" where keyname is the key that was not found. | ||||||||||||||||||||||||||||||||||||||||||||
PLEASE DO NOT USE try/except FOR THIS EXERCISE. Use an 'if' statement to see if the key is in the dict. Note that if Python can't find your file, it may be because the relative path is incorrect. Please see "Filepaths for Locating Files" in the Session 4 Slides. |
|||||||||||||||||||||||||||||||||||||||||||||
Sample program runs:
there are 50 pairs in the lookup dict
please enter a state name: California
Abbreviation for California is CA
|
|||||||||||||||||||||||||||||||||||||||||||||
there are 50 pairs in the lookup dict
please enter a state name: New York
Abbreviation for New York is NY
|
|||||||||||||||||||||||||||||||||||||||||||||
there are 50 pairs in the lookup dict
please enter a state name: Oman
no state found with name "Oman"
|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
See discussion for more detail.
|
|||||||||||||||||||||||||||||||||||||||||||||
5.4 | Lookup Dictionary with try/except (notations not required for this solution). Please resubmit your solution to the previous exercise, this time using try/except instead of 'if' to respond if the user types an invalid state name. With an invalid state name, Python should raise an exception -- if so, trap the exception and print the error message no state found with name "<keyname>" where <keyname> is the key that was not found. | ||||||||||||||||||||||||||||||||||||||||||||
YOU MUST USE try/except TO TRAP THE ERROR if the user types a bad key. Please do not use 'if' in this exercise. YOU MUST NOT USE except: by itself, nor except Exception: as these trap any type of exception. We must be specific when trapping exceptions. |
|||||||||||||||||||||||||||||||||||||||||||||
Sample program runs:
there are 50 pairs in the lookup dict
please enter a state name: California
Abbreviation for California is CA
|
|||||||||||||||||||||||||||||||||||||||||||||
there are 50 pairs in the lookup dict
please enter a state name: New York
Abbreviation for New York is NY
|
|||||||||||||||||||||||||||||||||||||||||||||
there are 50 pairs in the lookup dict
please enter a state name: Oman
no state found with name "Oman"
|
|||||||||||||||||||||||||||||||||||||||||||||
See discussion for more detail. HOMEWORK CHECKLIST: all points are required
|
|||||||||||||||||||||||||||||||||||||||||||||
5.5 | Ranking. Reading cities_green_space.csv, build a dictionary that pairs city name keys with "pct" float values. | ||||||||||||||||||||||||||||||||||||||||||||
{'Amsterdam': 13.0, 'Austin': 10.0, 'Barcelona': 28.0, 'Bogotá': 4.9,
'Brussels': 18.8, 'Buenos Aires': 9.4, 'Cape Town': 24.0, 'Chengdu': 42.3,
'Dublin': 26.0, 'Edinburgh': 49.2, 'Guangzhou': 19.78, 'Helsinki': 40.0,
'Hong Kong': 40.0, 'Istanbul': 2.2, 'Johannesburg': 24.0, 'Lisbon': 18.0,
'London': 33.0, 'Los Angeles': 34.7, 'Melbourne': 9.3, 'Milan': 13.74,
'Montréal': 12.82, 'Moscow': 18.0, 'Nanjing': 40.67, 'New York': 27.0,
'Oslo': 68.0, 'Paris': 10.0, 'Rome': 38.9, 'San Francisco': 13.0,
'Seoul': 27.91, 'Shanghai': 16.2, 'Shenzhen': 40.9, 'Singapore': 47.0,
'Stockholm': 40.0, 'Sydney': 46.0, 'Taipei': 6.56, 'Tokyo': 7.5,
'Toronto': 13.0, 'Vienna': 50.0, 'Warsaw': 17.0, 'Zürich': 41.0}
|
|||||||||||||||||||||||||||||||||||||||||||||
Next, sort the dict keys by value in reverse order, then loop through and print each city name and its pct value. |
|||||||||||||||||||||||||||||||||||||||||||||
Expected Output:
Cities Ranked by Greenspace (% of total area) Oslo 68.0 Vienna 50.0 Edinburgh 49.2 Singapore 47.0 Sydney 46.0 Chengdu 42.3 Zürich 41.0 Shenzhen 40.9 Nanjing 40.67 Helsinki 40.0 Hong Kong 40.0 Stockholm 40.0 Rome 38.9 Los Angeles 34.7 London 33.0 Barcelona 28.0 Seoul 27.91 New York 27.0 Dublin 26.0 Cape Town 24.0 Johannesburg 24.0 Guangzhou 19.78 Brussels 18.8 Lisbon 18.0 Moscow 18.0 Warsaw 17.0 Shanghai 16.2 Milan 13.74 Amsterdam 13.0 San Francisco 13.0 Toronto 13.0 Montréal 12.82 Austin 10.0 Paris 10.0 Buenos Aires 9.4 Melbourne 9.3 Tokyo 7.5 Taipei 6.56 Bogotá 4.9 Istanbul 2.2 |
|||||||||||||||||||||||||||||||||||||||||||||
5.6 | (Extra credit.) Summing dictionary. Reading FF_abbreviated.txt, build a dictionary that sums all of the Mkt-RF values (the 2nd column, or leftmost float values) associated with each year. Sort the dictionary's keys by value and print each key and corresponding value, so that the values sort ascending. Do not loop through the source data more than once. (You will loop through the sorted dict keys once you have built the dict.) | ||||||||||||||||||||||||||||||||||||||||||||
Sample program run:
1926: 3.39 1928: 3.88 1927: 4.67 |
|||||||||||||||||||||||||||||||||||||||||||||
(Note: these results are rounded - don't worry if yours are off by a small amount.)
|
|||||||||||||||||||||||||||||||||||||||||||||
Note that if Python can't find your file, it may be because the relative path is incorrect. Please see "Filepaths for Locating Files" in the Session 4 Slides.
See the discussion for more detail.
|
|||||||||||||||||||||||||||||||||||||||||||||
Sample program run:
please enter a number of results: 5 please enter a directory (ascending or descending): descending 1933: 53.89 1954: 40.06 1935: 38.15 1958: 35.96 1945: 32.55 |
|||||||||||||||||||||||||||||||||||||||||||||
These results show the best market years in history -- can you calculate the worst? (Note: these results are rounded at the very end during the loop in which we display the floats. If your numbers are off by a small amount, it may be due to your own computer's float precision.) Note: this solution must be completed without repeating any code within this solution! (Of course it will repeat much of the code in the regular credit solution.) HOMEWORK CHECKLIST: all points are required
|
|||||||||||||||||||||||||||||||||||||||||||||