user4100037
user4100037

Reputation:

Printing min and max function from input of a list

Every time I run the code I get "TypeError: 'int' object is not iterable".

So my question is: How do I print/use the min and max function at the end? So if someone let's say types 5,7,10, and -1. How do I let the user know that the highest score is 10 and the lowest score would be 5? (And then I guess organizing it from highest numbers to lowest.)

def fillList():

    myList = []

    return myList

studentNumber = 0

myList = []

testScore = int(input ("Please enter a test score "))

while testScore > -1:

      # myList = fillList()

      myList.append (testScore)

      studentNumber += 1

      testScore = int(input ("Please enter a test score "))

print ("")   
print ("{:s}     {:<5d}".format("Number of students", studentNumber))
print ("")
print ("{:s}           ".format("Highest Score"))
print ("")
high = max(testScore)
print ("Lowest score")
print ("")
print ("Average score")
print ("")
print ("Scores, from highest to lowest")
print ("")

Upvotes: 1

Views: 3342

Answers (2)

Torkoal
Torkoal

Reputation: 487

If all your scores are in a array.

print("The max was: ",max(array))
print("The min was: ",min(array))

Upvotes: 0

abarnert
abarnert

Reputation: 366103

Your problem is that testScore is an integer. What else could it be? Each time through the list, you reassign it to the next integer.

If you want to, say, append them to a list, you have to actually do that:

testScores = []
while testScore > -1:
    testScores.append(testScore)
    # rest of your code

And now it's easy:

high = max(testScores)

And, in fact, you are doing that in the edited version of your code: myList has all of the testScore values in it. So, just use it:

high = max(myList)

But really, if you think about it, it's just as easy to keep a "running max" as you go along:

high = testScore
while testScore > -1:
    if testScore > high:
        high = testScore
    # rest of your code

You will get different behavior in the case where the user never enters any test scores (the first one will raise a TypeError about asking for the max of an empty list, the second will give you -1), but either of those is easy to change once you decide what you actually want to happen.

Upvotes: 1

Related Questions