-2

I am a newbie to python programming. I am working on a class homework and got the code below so far. The next step that am struggling with is to write a function the would show / print the lowest score and average score. Any direction would be much appreciated.

scores = """Aturing:Mark$86:
Inewton:Mark$67.5:
Cdarwin:Mark$90:
Fnightingale:Mark$99:
Cvraman:Mark$10:"""

students = {}
for studentdata in scores.split('\n'):
data = studentdata.split(':')
name = data[0]
students[name] = {}
for class_data in data[1:]:
    if class_data:
        Mark,class_score = class_data.split('$')
        students[name][Mark] = class_score

def Grade_Show(student,Mark):
    if student in students:
        if Mark in students[student]:
            print "Student %s got %s in the assignment %s" % (student,students[student][Mark],Mark)
        else:
            print "subject %s not found for student %s" % (Mark,student)
    else:
        print "student %s not found" % (student)

#do some testing
Grade_Show("Inewton","Mark")
4

2 回答 2

0

The next step that am struggling with is to write a function the would show / print the lowest score and average score.

Step 1:

Can you iterate through your data structure (students) and print only the scores? If you can do that, then you should be able to run through and find the lowest score.

To find the lowest score, start with some imagined maximum possible value (set some variable equal to 100, for example, if that's the highest possible) and iterate through all the scores (for score in score..., etc.), testing to see if each value you get is lower than the variable you created.

If it is lower, make the variable you created equal to that lower value. After that, it will continue iterating to see if any new value is less than this new 'lowest' value. By the time it reaches the end, it should have provided you with the lowest value.

One tricky part is making sure to print both the name and lowest value, if that's what the question requires.

Step: 2

To solve the average problem, you'll do to something similar where you iterate over the scores, add them to a new data structure and then figure out how to take an average of them.

于 2013-11-02T17:31:14.550 回答
0

Testing with: scores = {'alex': 1, 'dave': 1, 'mike': 2};

Firstly, to find the lowest score, use the min() function.

So:

min_keys = [k for k, x in scores.items() if not any(y < x for y in scores.values())]

print('Lowest score:', str(min(scores.values())) + '.', 'Achieved by: ')
for student in min_keys:
    print(student)

Output:

Lowest score: 1. Achieved by: 
alex
dave

Secondly, assuming you are looking for the mean average, you would do this:

print('The average score was:', str(sum(scores.values()) / len(scores)))

Output:

The average score was: 1.3333333333333333

Hope I helped!- All you need to do now is create a function containing that code, with a parameter called data. That way you can have multiple dictionaries to represent different classes or tests. You would replace all instances of score in the code with data.

Also, the 'minimum score' code could be easily modified to give the maximum score. Finally, depending on the size of your program you could store the output in a variable rather than using a print statement so you can recall it later. This would also mean that you should return the result, not print it.

于 2013-11-02T18:18:03.727 回答