Project 6: Database
Rhodes College needs you to write a program to help the registrar manage their student
database. You will be creating a program that reads in a database of students and their
majors from a file, then lets the user perform various operations on the database.
How the program should work
Write a program that reads in the contents of the file database.txt, whose
file format is described below. Then, let the user choose from the following operations:
- Add a new student (and their major) to the database.
Ask the user to type in the name of the new student and their major.
Then add these items to your database (they will be lost when the program exits,
since they won't be saved to database.txt).
- Print a list of all the students in the database.
- Print a list of all the majors in the database (do not show duplicate majors more than once).
- Look up a student's major by their name.
Ask the user to type in the name of a student. If that student exists
in the database, the program should print their major. If the student doesn't
exist, the program should print an appropriate error message.
- Quit the program
The program should keep running until the user wants to quit.
The database file format
The database.txt file contains one line per student. Each line consists of the
student's first name, followed by a space, followed by their major. Neither
the name nor the major will have any spaces in them.
You can download database.txt, or get it from my public folder.
Sample interaction
What the user types is in bold, and brackets describe what
the program is doing.
[program begins, reads database.txt, and stores that information away]
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 2
All students: ['Joseph', 'Sally', 'Martha', 'Quentin', 'Patricia', 'Emma', 'Miguel']
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 3
All majors: ['Biology', 'Chemistry', 'Physics', 'CompSci', 'Art', 'ArtHistory']
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 4
What is the student's name? Sally
Sally is majoring in Chemistry.
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 4
What is the student's name? Mark
Student not found.
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 1
What is the new student's name? Mark
What is the new student's major? CompSci
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 4
What is the student's name? Mark
Mark is majoring in CompSci.
1. Add a new student
2. Print all students
3. Print all majors
4. Look up a student's major
5. Quit
Choose an option: 5
Done.
[program ends]
Hints
Try these steps:
- Write a program to read in the database.txt file into parallel lists --- one for
names, and one for the corresponding majors. These variables (names and majors) will
store all the information you need while the program is running. Print out the names
and majors lists to make sure they have the right information.
- Modify your program so that after you read in the database.txt file, you enter
a loop that asks the user for one of the five options. Make it so when they want to quit,
the program ends.
- Write code to implement option 2 (print out the list of all students).
- Write code to implement option 3 (print out the list of all majors). This will be
similar to option 2, but I suggest writing a function that will give you a copy of
a list with duplicates removed. I.e., "def remove_dups(lst):" that takes a list
and returns a copy of that list with duplicates removed.
- Write code for option 4. Use the lookup idea for parallel lists we've talked about
in class.
- Write code for option 1. This is the only place that your names and majors lists
can change, outside of the code that reads database.txt at the beginning.
Extra credit
Add in an option to print all of the students who are majoring in a given subject.
For instance, the user could type in Biology and the program would report that
Joseph and Quentin are majoring in Biology.
What to turn in
Through Moodle, turn in your code as a file called database_yourLastName_yourFirstName.py.