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.
Next, write the following functions and make sure they work:
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.)
Takes the names and grades lists as arguments, along with a name to look up. Searches the names list for the name specified, and returns that person's grade. If the name isn't found, returns -1 (negative one).
Example from the python shell:
(assume names and grades are defined as above)
>>> lookup_grade(names, grades, "sally") should return 80
>>> lookup_grade(names, grades, "bob") should return -1
>>> lookup_grade(names, grades, "fred") should return 95
Next, using your functions from above (you can define others if you want), write a main function that does the following things. First, use a line like names, grades = read_gradebook() to read in the gradebook.txt file. Second, print out a menu that lets 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? jesse 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: jesse 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 jesse 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 hte highest/lowest grade.