【Python】Student Becomes the Teacher

Student Becomes the Teacher

Use what you’ve learned so far to manage your own class.

Lesson Number One

Welcome to this “Challenge Course”. Until now we’ve been leading you by the hand and working on some short and relatively easy projects. This is a challenge so be ready. We have faith in you!

We’re going to switch it up a bit and allow you to be the teacher of your own class. Make a gradebook for all of your students.

The example above is just to remind you how to create a dictionary and then to access the item stored by the "cat" key.

  1. Create three dictionaries: lloyd,alice, and tyler.
  2. Give each dictionary the keys "name","homework""quizzes", and "tests".
  3. Have the “name” key be the name of the student (that is, lloyd‘s name should be "Lloyd") and the other keys should be an empty list. (We’ll fill in these lists soon!)

What’s the Score?

Great work!


Now fill out your lloyd dictionary with the appropriate scores. To save you some time, we’ve filled out the rest for you.

Homework: 90.0, 97.0, 75.0, 92.0
Quizzes: 88.0, 40.0, 94.0
Test Scores: 75.0, 90.0

Make sure to include the decimal points so your grades are stored as floats! This will be important later.

Put It Together

Now lets put the three dictionaries in a list together.

The above example is just a reminder on how to create a list. Afterwards, my_list contains 12, and 3.


Below your code, create a list called students that contains lloydalice, and tyler.

For the Record

Excellent. Now you need a hard copy document with all of your students’ grades.

The example above is just to remind you how to create a dictionary and then to access the item stored by the "cat" key.


for each student in your students list, print out that student‘s data, as follows:

  1. print the student‘s name
  2. print the student‘s homework
  3. print the student‘s quizzes
  4. print the student‘s tests

It’s Okay to be Average

When teaching a class, it’s important to take the students’ averages in order to assign grades.

The above example is a reminder of how division works in Python.

  1. When you divide an integer by another integer, the result is always an integer (rounded down, if needed).
  2. When you divide a float by an integer, the result is always a float.
  3. To divide two integers and end up with a float, you must first use float() to convert one of the integers to a float.

Write a function average that takes a list of numbers and returns the average.

  1. Define a function called average that has one argument, numbers.
  2. Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.
  3. Like the example above, use float()to convert total and store the result intotal.
  4. Divide total by the length of thenumbers list. Use the built-in len()function to calculate that.
  5. Return that result.

Don’t use list as a parameter name because it is a special function in Python. Many people also say you shouldn’t use l because it looks too much like a 1.

Just Weight and See

Great! Now we need to compute a student’s average using weighted averages.

  1. In the above example, we create a dictionary called cost that contains the costs of some fruit.
  2. Then, we calculate the average cost of apples and the average cost of bananas. Since we like apples much more than we like bananas, we weight the average cost of apples by 90% and the average cost of bananas by 10%.

The \ character is a continuation character. The following line is considered a continuation of the current line.


Write a function called get_average that takes a student dictionary (like lloyd,alice, or tyler) as input and returns his/her weighted average.

  1. Define a function called get_averagethat takes one argument called student.
  2. Make a variable homework that stores the average() of student["homework"].
  3. Repeat step 2 for “quizzes” and “tests”.
  4. Multiply the 3 averages by their weights and return the sum of those three. Homework is 10%, quizzes are 30% and tests are 60%.

Sending a Letter

Great work!

Now let’s write a get_letter_grade function that takes a number score as input and returns a string with the letter grade that that student should receive.

  1. Define a new function called get_letter_grade that has one argument called score. Expect score to be a number.
  2. Inside your function, test score using a chain of if: / elif: / else:statements, like so:

    If score is 90 or above: return "A"
    Else if score is 80 or above: return "B"
    Else if score is 70 or above: return "C"
    Else if score is 60 or above: return "D"
    Otherwise: return "F"

  3. Finally, test your function! Call yourget_letter_grade function with the result of get_average(lloyd). Print the resulting letter grade.

Your function will probably contain a bunch of elif statements to figure out what to do with each grade. It might look something like this:

Your final print statement should be:

Part of the Whole

Good! Now let’s calculate the class average.

You need to get the average for each student and then calculate the average of those averages.

  1. Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.
  2. First, make an empty list called results.
  3. For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
  4. Finally, return the result of callingaverage() with results.

How is Everybody Doing?

Awesome! You’re doing great.

  1. Finally, print out the result of calling get_class_average with your students list. Your students should be [lloyd, alice, tyler].
  2. Then, print the result of get_letter_grade for the class’s average.

3 thoughts on “【Python】Student Becomes the Teacher

  1. cheap nfl jerseys

    I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and amusing, and without a doubt, you’ve hit the nail on the head.
    The problem is something which not enough folks are speaking
    intelligently about. I’m very happy I found this during
    my hunt for something relating to this.cheap nfl jerseys



电子邮件地址不会被公开。 必填项已用*标注