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.
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.
Step 2: Begin a new Python program and write the following function that will read the gradebook file into two parallel lists:
Opens "gradebook.txt" and creates two parallel lists, called names and grades, containing the appropriate information from gradebook.txt.
Use a for loop to read in each line of the file, strip the newline away, split the line with using the split function (on a colon), and store each piece into the appropriate list. At the end of your function return BOTH lists by using the command return names, grades. Note: this is the first time we've seen a way to return more than one thing from a function.
Example from the python shell:
>>> read_gradebook()
should return (['phil', 'sally', 'mary', 'fred', 'joe'], [90, 80, 95, 85, 89])
(Don't worry about the weird looking return value; if you do "return names, grades"
everything will work fine.)
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:
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).
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.
[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]
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.