-3

So far, the program finds the average and prints it out. However, I'm not sure how to include the max & the min.

Need to find the max & the min of the values and minus them before calculating the average.

Question reads: Read in integers until the user enters -1. If there were at least 3 values, show the average excluding the biggest and smallest number. If there are less than 3 values, print nothing.

value = int(input("Value: "))
count = 0
sum = 0
max = 0
min = 0

while value != -1:
    sum += value
    count += 1
    value = int(input("Value: "))

if count >= 3:
    average = sum / count
    print ("Middle average =", average - max - min)
  • 2
    How about something that starts with `if value > min: ...` and `if value < max:` in that loop...? :) – AKX Aug 25 '22 at 05:53
  • 1
    You may be better reading the input into a [list](https://developers.google.com/edu/python/lists) [(python documentation)](https://docs.python.org/3/tutorial/datastructures.html), sorting it with [`sort()`](https://docs.python.org/3/howto/sorting.html) and reading the last and first value of the list. – Mime Aug 25 '22 at 06:00
  • Please avoid using sum, max, and min as variable names as they are also python built-in function names. – Jobo Fernandez Aug 25 '22 at 06:15

2 Answers2

0

You've already declared two integers, max and min. All that's left is to keep track of them as you input the numbers, updating them with each input.

Here is an implementation. Note I have renamed your max and min variables as they are the names of builtin functions already.

Also note that initially both maxValue and minValue are set to the first value.

value = int(input("Value: "))
count = 0
sum = 0
maxValue = value
minValue = value

while value != -1:
    sum += value
    maxValue = max(maxValue, value)
    minValue = min(minValue, value)
    count += 1
    value = int(input("Value: "))

if count >= 3:
    average = (sum - maxValue - minValue) / (count - 2) # exclude the two values here
    print ("Middle average =", average)
Ryan Zhang
  • 1,856
  • 9
  • 19
0

You can try this solution:

in_list = []

while True:
    inpt = int(input('Value:'))
    if inpt != -1:
        in_list.append(inpt)
    else:
        break

if len(in_list) >= 3:
    in_list.remove(max(in_list))
    in_list.remove(min(in_list))
    print("Middle average: ", sum(in_list)/len(in_list))
else:
    print('Nothing')

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Python29
  • 112
  • 6