1

I have to create individual scores from the keyboard and store them in a list. Then call each of the three functions,passing the list,to compute the average, highest, and lowest score. ##note create 3 functions (ave,high,low),and pass the list to each

What am I doing wrong??

I'm a mess right now with this sorry for all the extra stuff!

def getScores():
    """This function asks the user for list of scores
    """
    validNums=("0123456789")
    lstScores=[ ]
    strNum="0"
    while len(strNum) > 0:
         strNum=raw_input("Enter a number or press ENTER when done: ")
        if len(strNum) > 0:
            for digit in strNum:
                if digit in validNums:
                    lstScores.append(int(strNum))
                else:
                    print "invalid data entered!"

    return lstScores

def DataSort(sortedScores=[ ]):
    sortedScores.sort()
    return sortedScores

def Average(avgScores=[]):
    sum(lstScores) / len(lstScores)
    for avg in avgScores:
        print avgScores

    return avgScores


##def DataPrint(scoresToPrint=[ ]):
##    for score in scoresToPrint:
##        print score ####list sort

 ###MAINLINE
 lstScores=getScores()
 ##sortedScores=DataSort(sortedScores=lstScores)
 avgScores=Average()
 ##DataPrint(scoresToPrint=sortedScores)
print
print lstScores
print
print avgScores
##print sortedScores
##for num in lstScores:
##    print num

##for avg in avgScores:
    ##print avg
  • What's going wrong for you? Not getting the right output? Not getting any output? etc... – smessing Mar 21 '12 at 00:35
  • So I can get the list to go through but when I try to get the average it says none. – user1282227 Mar 21 '12 at 00:37
  • or just gives me [] its not giving me any output for the average – user1282227 Mar 21 '12 at 00:38
  • 1
    What is `lstScores`? It's not passed to `Average`. Is it a global? Also, as someone else mentioned in a deleted answer, you do a bunch of stuff with `lstScores` but you don't store the result anywhere. In general, your `Average` function is strange. And finally, is this homework? If so, tag it as homework. – senderle Mar 21 '12 at 00:39
  • 1
    Independent of the issues involving how and when to pass arguments to functions, the logic of getScores() looks off to me. It will currently append an entered number (like `123`) once for every digit, which I don't think is what's intended. – DSM Mar 21 '12 at 00:45
  • Don't use a list as a default function argument: http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument – Daenyth Mar 21 '12 at 03:12

2 Answers2

0

It's not clear why you pass a default value for avgScores in the function Average, but this will work for finding the average:

def Average(lstScores):
    return float(sum(lstScores)) / len(lstScores) if lstScores else 0.0

Call it like this:

Average(lstScores)

Also, I'm assuming scores are in the range 0-9, if a score is greater than 9 the validation logic in getScores() will fail.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

You have several problems.

  1. You're adding numbers multiple times to your list.
  2. You're computing the average of an empty list! You need to pass lstScores to the function Average(). I've fixed the code for you, but now it'll throw a ValueError if you write a non-integer number. I leave it to you to figure out the exception handling.
  3. Your Average() function didn't actually compute the average...

    def getScores():
        """This function asks the user for list of scores
        """
        validNums=("0123456789")
        lstScores=[ ]
        strNum="0"
        while len(strNum) > 0:
            strNum=raw_input("Enter a number or press ENTER when done: ")
            if len(strNum) > 0:
                lstScores.append(int(strNum))
    
        return lstScores
    
    def DataSort(sortedScores=[ ]):
        sortedScores.sort()
        return sortedScores
    
    def Average(avgScores=[]):
        avg = sum(lstScores) / float(len(lstScores))
        return avg 
    
    lstScores=getScores()
    
    avgScores=Average(lstScores)
    
    print lstScores
    print "Average of scores: " + str(avgScores)
    
smessing
  • 4,330
  • 1
  • 22
  • 19