0

I have an assignment where I let the user input as many scores and then I have to calculate the minimum and maximum value along with how many people got those scores using for-loop. I've calculated the average and the standard deviation:

elif user_option == 3:

    total = 0
    for val in scores_list:
        total = total + val
    average = total/ len(scores_list)
    print (average)
elif user_option == 2:
    total = 0
    for val in scores_list:
        total = total + val
    average = total/ len(scores_list)
    diffsquared = 0
    sum_diffsquared = 0
    for val in scores_list:
        diffsquared= (val - average)**2
        sum_diffsquared= diffsquared + sum_diffsquared
    stdev= sqrt((sum_diffsquared)/len(scores_list))
    print(stdev)

Any ideas how to find the min and max values?

3
  • Some reformatting is in order :/. Also give a short, standalone and runnable sample. Commented Sep 29, 2013 at 21:31
  • Also, spaces are preferred over tabs in python Commented Sep 29, 2013 at 21:40
  • Is there a first part to your if statement? Because you can't start with elif... Commented Sep 29, 2013 at 21:40

2 Answers 2

1

Something like this?

min_val = float("inf")
max_val = -float("inf")
count_min = 0
count_max = 0

for val in scores_list:
    if val < min_val:
        min_val = val
        count_min = 1
    elif val == min_val:
        count_min += 1

    if val > max_val:
        max_val = val
        count_max = 1
    elif val == max_val:
        count_max += 1

print "Minimum score:", min_val
print "Maximum score:", max_val
print "Number of students with minimum score:", count_min
print "Number of students with maximum score:", count_max

EDIT: As @GL770 has noted in the comments, sys.maxint is only available in Python 2.x. In Python 2.x you could have done something like this.:

import sys
min_val = sys.maxint
max_val = -sys.maxint - 1

The float("inf") thing also works in Python 2.x though so this method is not required.

Sign up to request clarification or add additional context in comments.

4 Comments

@GL770 Thanks for letting me know. I made it float("inf") and -float("inf") instead. That should be Python 3 compatible.
I don't think your count_min, count_max values are correct. Think about: scores = [6,6,6,7,7,7,8,8,9,3,3,3,2,2,2,1,1]
@bcollins yeah theres a reason because in your code the count_min never gets reset to 1 if it finds a new minimum value. Also try running my code with that list. It works. For me it printed: Minimum score: 1 Maximum score: 9 Number of students with minimum score: 3 Number of students with maximum score: 1
Well in your edited list it prints: Minimum score: 1 Maximum score: 9 Number of students with minimum score: 2 Number of students with maximum score: 1 which is correct. I don't believe there are any issues with my code in terms of correctness.
0

How about the built-in functions min() and max():

scores_min = min(scores_list)
scores_max = max(scores_list)

also don't forget about numpy:

import numpy
scores_array = numpy.array(scores_list)
scores_mean = numpy.mean(scores_array)
scores_std = numpy.std(scores_array)
scores_min = numpy.min(scores_array)
scores_max = numpy.max(scores_array)

3 Comments

I can't use the built in functions. I have to use loops
I'm fairly certain he's not allowed to use built-ins or numpy.
ahh ok sorry. This is just an academic exercise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.