Write a program that requests some grades as inputs, and displays the average after dropping the smallest and the largest grades! Your program should stop asking the user for a new grade when the user typed any negative number.
Here is a sample run:
Enter a grade?40
Enter a grade?50
Enter a grade?60
Enter a grade?70
Enter a grade?80
Enter a grade?90
Enter a grade?100
Enter a grade?-1
70.0
Note that in this example 40 is the lowest grade and 100 is the largest grade, and therefore are dropped, then the average is (50+60+70+80+90)/5=70.
Do this in python
The code is given below-
lst=[]
count=0
n=int(input("Enter a grade?"))
if(n>=0):
while(n>=0):
lst.append(n) #entering the values in the list
count=count+1 #counting the number of elements
n=int(input("Enter a grade?"))
max_value=max(lst) #finding max value from list
min_value=min(lst) #finding min values
#print(max_value)
count=count-2 #removing count for max and min value
sum_value=0
lst.remove(max_value) #removing max value
lst.remove(min_value) #removing min value
if(count>0):
for i in lst:
# print(i)
sum_value=sum_value+i #summing up the values
avg=sum_value/count #calculating average
print(avg)
else:
print('0')
the screenshot is attached below-
Get Answers For Free
Most questions answered within 1 hours.