COMP 141: Project 8

Gradebook

Your professor needs help managing his grades for CS 141. He has asked you to write a gradebook application that will store a list of students along with their corresponding final course grades (which are integers).

You decide to design your program by using two parallel lists that will always be the same length. One list, called names, will store the first names of the students. The second list, called grades, will store the grades. You decide to always keep the lists managed in such a way that the person whose name is in names[0] will always have their corresponding grade in grades[0], and the same thing for names[1] and grades[1], etc. In other words, the two lists are related to each other the same way as the english_words and pirate_words lists were related to each other in the pirate translator.

How the program should work

This is a general description of the program. See details later for more instructions.

The program will read in a list of students and grades from a file called gradebook.txt. Then, the program will enter a loop where a menu will be displayed that lets the user pick from a number of options including adding a new student/grade pair, printing all students or all grades, searching the gradebook for a student's grade, or finding the student with the highest or lowest grade. There is also an option to quit the program.

What you need to do

Step 1: Download gradebook.txt and put it in the same directory as your code. Each line of this file contains a student's name and their corresponding numeric class grade, separated by a colon.

Step 2: Begin a new Python program and write the following function that will read the gradebook file into two parallel lists:

Step 3: Write a main() function that calls read_gradebook() to capture the students and grades lists. Because read_gradebook() returns two variables, you can capture them with a line of code like this:

names, grades = read_gradebook()

Step 4: Add a loop to main() that prints a menu repeatedly until the user wants to quit. Let the user choose from the following operations:

  1. Add a new student (and their grade) to the gradebook.

    Ask the user to type in the name of the new student and their grade. Then add these items to your gradebook (they will be lost when the program exits, since they won't be saved to gradebook.txt).

  2. Print a list of all the students in the gradebook.
  3. Print a list of all the grades in the gradebook.
  4. Look up a student's grade by their name.

    Ask the user to type in the name of a student. If that student exists in the gradebook, the program should print their grade. If the student doesn't exist, the program should print an appropriate error message.

  5. Print the name of the student who has the highest grade in the class, along with their grade.
  6. Print the name of the student who has the lowest grade in the class, along with their grade.
  7. Quit the program
The program should keep running (displaying the menu) until the user wants to quit. Do not read in the gradebook each time the menu is displayed; that will overwrite any new students and grades that the user enters. Instead, make sure the read_gradebook() function is only called at the beginning of main, not inside your loop for the menu.

Sample interaction

[program begins]
1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 2
All students:  ['phil', 'sally', 'mary', 'fred', 'joe']

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 3
All grades:  [90, 80, 95, 85, 89]

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 4
Look up which student? sally
Student sally has grade 80

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 4
Look up which student? jessie
That student doesn't exist.

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 1
New student name: jessie
New student grade: 99

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 5
The student with the highest grade is jessie
Their grade is 99

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 6
The student with the lowest grade is sally
Their grade is 80

1. Add a new student & grade
2. Print all students
3. Print all grades
4. Lookup a student's grade
5. Find student with highest grade
6. Find student with lowest grade
7. Quit
Choose an option: 7
Done.
[program ends]

Hints

For the highest/lowest grade, you will need to re-visit the code we wrote that finds the maximum or minimum element in a list. However, you need to find not only the element, but *its position*. That way, you can find the corresponding element in the names list. For instance, when you find the minimum item in the grades list, you'll find 80. However, you also need the *position* of the 80, which is 1, because names[1] is sally.

Do not use sort(), min(), or max() to solve any of these problems. Write your own loops. Also, do not use find() or index() to solve the highest/lowest grade problems. Use the idea from class of writing code to find the position in the lists of the highest/lowest grade.

Challenge Problems

What to turn in

Through Moodle, turn in your code as a file called gradebook_yourLastName_yourFirstName.py.