Okay, we are working with dictionaries here. Lets say you have these dictionarie
ID: 3547485 • Letter: O
Question
Okay, we are working with dictionaries here.
Lets say you have these dictionaries.
Student_IDs= {} #keys are the IDs, values are semicolon split
Name = {} #keys are student (last,first), values are the original line
Major = {} #keys are the major, values are the IDs
Major2 = {} #keys are the major, values are the student names
Work = {}
IDs = []
You are reading in information from a file,
Williams, Serena; 8453392; 02/14/1994; Math;3.09; 5 Gaga, Lady; 7733469; 10/10/1980; Computer Science; 1.20; 40
(just a few lines of the file)
I have the dictionaries built. My issue is #1, compute the average.
If you can write a program (you don't need to build the dicitonaries if you can do it mentally without) that computes #1 you get the points.
I just need to know how to compute the average using my dictionaries.
1) Compute the average GPA of all students with a particular major. The program then prints out all the different majors that are in the file, and asks the user to enter one. Then the program does the calculations and prints the information requested.
Explanation / Answer
I'll give you the steps if that's what you want..
I can write the code as well for it... i dont mind..
Steps to be followed..
step 1 : Determine all the ids of students from your Major dictionary..
step 2: now that you have a list of all the ids.. our main goal is to find the gpas of all those ppl from that major..
store the length of the list in a variable n, and initiate a variable called sum = 0
step 3: now pick one id from the list.. add their GPA to your sum and iterate over the list..
Step 4: Now computing average is left.. that is the sum/n.. n here means the number of ppl belonging to that major
Hope this helps..
Do mention it.. if you want the code for it
The code to your question looks like this..
major_name = input()
list_of_ids = Major[major_name]
sum_of_gpa = 0
number_of_students = len(list_of_ids)
for i in list_of_ids:
sum_of_gpa += i[-2] #-2 here gives us the last but one elements value
average_gpa = sum_of_gpa/number_of_students
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.