CSE 111: Programming with Functions

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:

  1. 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.
  2. Get an ID Number from the user, use the ID Number to find the corresponding student name in the dictionary, and print the name.
  3. 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.

  1. 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.
  2. When a user enters an ID Number, your program should ensure it is a valid ID Number.
    1. If there are too few digits in the ID Number, your program should print, "Invalid ID Number: too few digits" (without the quotes).
    2. If there are too many digits in the ID Number, your program should print, "Invalid ID Number: too many digits" (without the quotes).
    3. If the given ID Number contains any characters besides digits and dashes, your program should output "Invalid ID Number" (without the quotes).
  3. 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

  1. Create a folder for this week's code-along program.
  2. Download the students.csv file and save it in the folder you just created.
  3. Open the folder you just created in VSCode.
  4. Create a new file named students.py.
  5. 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
Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. Download the test_students.py Python file and save it in the same folder where you saved your students.py program. Run the test_students.py file and ensure that the test_read_dictionary function passes. If it doesn't pass, there is a mistake in your read_dictionary function. Read the output from pytest, fix the mistake, and run the test_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 =====================
                
  2. 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
  3. 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?
  4. 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:

Useful Links: