问题描述:

I'm trying to open a text file in python that holds the names of people and grades from specific tests they took. These grades are also accompanied by the test weight.

For example:

Billy Bother 20 89 30 94 50 82

Hermione Heffalump 40 93 60 97

Kurt Kidd 20 88 30 82 40 76 10 99

The first number is the weight, while the second number is the grade, and this pattern continues through the file.

To calculate the average I need to use ((w1 * g1) + (w2 * g2) + ... + (wn * gn)) / 100, where w = weight and g = grade.

I have no idea how to set up the code to make this work.

Can anyone help me?

网友答案:

I'm assuming this is a school assignment, so I will just give an outline of how to approach it, rather than writing a solution for you.

You will need to find out how to:

  1. Read a text file and loop over the lines, so each line becomes a string of text
  2. For each line, split it into fields (Billy, Bother, 20, ...)
  3. Pull out the first two fields - these are the names
  4. Loop over the rest of the fields, reading two at a time (w,g)
  5. Multiply w * g and add the result to a total
  6. Once all the numbers are processed, divide by 100

You should be able to find solutions to all of these sub-problems in a Python tutorial, book, Internet search or Stack Overflow - but if not, come back and ask a more specific question, showing your attempted code and the problem you are having with it.

相关阅读:
Top