W05 Code-along Activity: CSV Files
Code-along Activity Overview
For this activity, you will code along with an instructor video that walks you through each step of the activity below. Make sure that you pause the video, or watch sections over if you need to, so that you are able to complete the entire activity.
Your instructor will post a link to the video in Microsoft Teams.
Background
A common task for many knowledge workers is to use a number, key, or ID to find information about a person. For example, a knowledge worker may use a phone number or e-mail address as a key to find (or look up) additional information about a customer. During this activity, your team will write a Python program that uses a student’s ID Number to look up the student’s name.
Program
This program will read data from a comma separated file (csv) of student information into a dictionary. This dictionary will then be used to lookup student information by ID number.
Requirements
Your program must do the following:
-
Open the
students.csv
file for reading, skip the first line of text in the file because it contains only headings, and read the other lines of the file into a dictionary. The program must store each student ID Number as a key and each ID Number name pair or each name as a value in the dictionary. -
Get an ID Number from the user, use the ID Number to find the corresponding student name in the dictionary, and print the name.
-
If a user enters an ID Number that doesn’t exist in the dictionary, your program must print the message, "No such student" (without the quotes).
Enhancements
Here is a list of enhancements that you could make to the program. Your instructor will walk you through at least one of them. Feel free to complete others.
-
Add code to remove dashes from the ID Number that the user enters. This will allow the user to enter ID Numbers with dashes or without dashes and still allow the computer to search in the dictionary.
-
When a user enters an ID Number, your program should ensure it is a valid ID Number.
-
If there are too few digits in the ID Number, your program should print, "Invalid ID Number: too few digits" (without the quotes).
-
If there are too many digits in the ID Number, your program should print, "Invalid ID Number: too many digits" (without the quotes).
-
If the given ID Number contains any characters besides digits and dashes, your program should output "Invalid ID Number" (without the quotes).
-
-
Add something or change something in your program that you think would make your program better, easier for the user, more elegant, or more fun. Be creative.
Instructions
- Create a folder for this week's code-along program.
- Download the
students.csv
file and save it in the folder you just created. - Open the folder you just created in VSCode.
- Create a new file named students.py.
- Find the video that your instructor has posted for this assignment and code along with them to complete each of the requirements. Work through the requirements in order with the instructor rather than jumping ahead to more complicated steps to ensure you do not miss any concepts.
Alternative Video Link and Transcript
In addition to the link that your instructor posted, you are also welcome to watch the following code walkthrough or use its transcript:
Additional Resources
You may use these additional resources for further study.
Sample Solution
In addition to the instructor video, you can also view this solution. (This solution may be slightly different than the final code that you and your instructor created.)
Helpful Documentation
-
The Reading Files article explains how to setup VS Code so that your Python program can read from a text file.
-
The preparation content for this lesson includes a section about reading a CSV file into a compound dictionary.
-
The preparation content for week 4 explains how to use dictionaries.
-
Your program can call the string replace method to replace all occurrences of a character with another character or to remove all occurrences of a character by replacing them with the empty string ("").
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
-
Download the
test_students.py
Python file and save it in the same folder where you saved yourstudents.py
program. Run thetest_students.py
file and ensure that thetest_read_dictionary
function passes. If it doesn't pass, there is a mistake in yourread_dictionary
function. Read the output frompytest
, fix the mistake, and run thetest_students.py
file again until the test function passes.> python test_students.py =================== test session starts ==================== platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy rootdir: C:\Users\cse111\week05 collected 1 item test_students.py::test_students PASSED [100%] ==================== 1 passed in 0.12s =====================
-
Run your program and enter the inputs shown below. Ensure that your program’s output matches the output below.
> python students.py Please enter an ID Number (xxxxxxxxx): 551234151 No such student > python students.py Please enter an ID Number (xxxxxxxxx): 751766201 James Smith
-
Run your program and enter this ID Number as input:
00-115-2306
(including the dashes). Many users will want to enter ID Numbers with dashes. How should your program handle the dashes? -
Run your program and enter an ID Number with too few digits or too many digits. How should your program handle these invalid ID Numbers?
Submission
Please pause and re-watch any sections of the instructor video necessary until you have completed the entire activity and your program runs the same as the instructor's. When you are finished:
- Return to Canvas to take the quiz.
Useful Links:
- Return to: Week Overview | Course Home