-2

I am working on the following, I created a dictionary object as follows and I need to define 3 different functions. A function for maxScore, a function for minScore, and function for average as well as print all 3.

My dictionary object code is as follows

scores = {}
scores['Andy'] = 78
scores['Bill'] = 82
scores['Cindy'] = 94
scores['Dave'] = 77
scores['Emily'] = 82
scores['Frank'] = 94
scores['Gene'] = 87

I can't seem to define the function(s) correctly for it to print the results.

E-Arsenal
  • 7
  • 1
  • 2
  • 2
    Possible duplicate of [Getting key with maximum value in dictionary?](https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary) – Dadep Jul 12 '17 at 17:50
  • @AGNGazer Without it (or something similar), `scores['Andy'] = 78` will raise a `NameError`. – chepner Jul 12 '17 at 17:55

5 Answers5

5

use min() and max :

>>> scores = {}
>>> scores['Andy'] = 78
>>> scores['Bill'] = 82
>>> scores['Cindy'] = 94
>>> scores['Dave'] = 77
>>> scores['Emily'] = 82
>>> scores['Frank'] = 94
>>> scores['Gene'] = 87

>>> max(scores, key=scores.get)
'Frank'
>>> min(scores, key=scores.get)
'Dave'

and for the average :

>>> sum(scores[i] for i in scores)/len(scores)
84
Dadep
  • 2,796
  • 5
  • 27
  • 40
1

For max:

def max_val(scores):
    return max(scores.values())

min

def min_val(scores):
    return min(scores.values())

and avg

    def avg_val(scores):
        return sum(scores.values())/len(scores)
rma
  • 1,853
  • 1
  • 22
  • 42
  • If `min` and `max` are both just returning a value, you don't need to know the key. Just use `max(scores.values())`. (Also, you've inadvertently created an infinite recursion situation by re-using `min` and `max` as your function names.) – chepner Jul 12 '17 at 17:53
  • You're completely right. That's embarrassing. Edited answer to reflect changes. – rma Jul 12 '17 at 17:55
  • Ditto for `avg_val`; you don't need the key. You don't need the loop, either: `sum(scores.values())` gets the sum. Although, it is debatable which is better: compute a running calculation of the length while iterating, or iterate twice implicitly (in `sum` and `len`). – chepner Jul 12 '17 at 17:56
  • I noticed that as well and fixed it. Thanks for the feedback! – rma Jul 12 '17 at 17:57
  • Thank you, that worked, I am just starting out and this one tripped me up. – E-Arsenal Jul 12 '17 at 18:21
  • No problem! Make sure to accept someone's answer so people know the question is resolved :) – rma Jul 12 '17 at 18:24
0

Max Score:

def maxScore(scDict):
  max = scDict[0]
  for i in scDict:
    if scDict[i]>max:
      max=scDict[i]
  return max

Min Score:

def minScore(scDict):
  min = scDict[0]
  for i in scDict:
    if scDict[i]<max:
      min=scDict[i]
  return min

Avg Score:

def avgScore(scDict):
  avg = 0. # python 2
  for i in scDict:
    avg+=scDict[i]
  return avg/len(scDict)

You can also use something like sys.maxsize in case of minScore (Py 3.0) if you desire.

anugrah
  • 187
  • 1
  • 1
  • 10
0

try this:

best_scores = sorted(scores, key=scores.get, reverse=True)
maxScore = scores[best_scores[0]]
minScore = scores[best_scores[-1]]
average =  sum(scores.values())/len(scores.values())
efirvida
  • 4,592
  • 3
  • 42
  • 68
0
sorted(scores.items(), cmp=lambda x, y: x[1] - y[1])
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45