In this exercise, you are going to create
a program that grades a student's test scores.
You'll see on lines 1 to 7, that we've got a dictionary,
and it's called student_scores.
Inside the dictionary,
the keys are the names of our students,
and the values are their test scores.
What you are going to do is using a for loop to loop
through the dictionary, you're going to write a program
that can interpret each student's scores
and create a new dictionary called student_grades
and in this dictionary, you have the student names matching
with their corresponding grades.
If you take a look inside the Description pane,
you will see the scores and their corresponding grades.
So for example, a score of 91 to 100 is interpreted
as Outstanding, 81 to 90 is Exceeds Expectations,
and so on and so forth.
Take a look at the Expected Output and see if you can
figure out how to use a loop on a dictionary
in order to make your program work as expected.
Give it a go now.
The first thing I'm going to do is to create a dictionary
called student_grades
and simply have it as an empty dictionary { }.
This is what we're going to fill up using our program.
Next, I'm going to create a for loop that loops through
the dictionary of student_scores so that I can get hold
of each of the keys in that dictionary one by one.
In order to access that student_score,
I have to use the key on the student_scores dictionary.
So exact line of code looks like line 14 where the score,
the variable that I'm interested in storing
is equal to student_scores,
passing in each of the student names as the key,
and now what the score will equal is the first time
my loop runs, it'll equal to 81.
Next time it'll be 78.
Next time it'll be 99, and so on and so forth.
Now comes the easier part where I have my if,
elif, else statements, and I simply use the criteria
that's in the Description pane to determine how each score
is going to be interpreted.
So if the score is greater than 90,
then I can take the student_grades,
which is our empty dictionary, empty for now,
and then use the square brackets [ ] to assign the student,
which is what we're looping on at the moment.
That's going to be the name of the student,
and set it to equal a value, which is going to be the grade.
And I can do that if the score is greater than 80
and if the score is greater than 70.
And remember, because the way that if, elif, else statements work,
is they work like a waterfall.
So if the first one is false,
then it goes to the second one.
So even though we're only checking on line 17,
that elif score is greater than 80
because the first one already went into the elif
that means the first condition on line 15 is already False.
So that basically means that the score that we are assigning
exceeds expectations on 18 is between 80 and 90.
Now, the way that dictionaries work with the key
and the value and how you access a value
through the key can be a little bit mysterious
but if you use print statements
and evaluate what your beliefs and reality and compare them
then you should be able to get
to a point where you can solve the puzzle and figure out
how to make the program do what you want it to.
So if you needed to make any fixes now,
go ahead and do that.