Home > front end >  How do I drop one of the inputs on this for loop?
How do I drop one of the inputs on this for loop?

Time:02-01

I am doing an assignment where I need to calculate an average grade, in one part I have a for loop where it takes the input of 5 quiz grades, I can't figure how to drop the lowest grade out of the ones entered during calculation.

print("Please enter 5 quiz grades.")
print("\t")
for i in range(5):
    quizgrade = float(input("Quiz grade: "))
    quizgradetotal  = quizgrade
print("\t")

here is the code so far.

I have tried changing the loop, but I can't figure it out.

CodePudding user response:

One of the ways you can approach this is to store each entered quiz grade in a list. Then you can drop the lowest grade once you know all of the grades:

quizgrades = [] # Initialize an empty list
print("Please enter 5 quiz grades.")
print("\t")

# Grab all 5 quiz grades
for i in range(5):
    quizgrades.append(float(input("Quiz grade: ")))

# Now remove the lowest number from the list
quizgrades.remove(min(quizgrades))

# Now that you have a list with the lowest grade dropped, you can easily calculate the average

average = sum(quizgrades) / len(quizgrades)
 
print(average)

CodePudding user response:

one solution is to store all your inputs in one array

gards = [] 
for i in range(5):
    gards.append(float(input("Quiz grade: ")))

then remove the lowest grade :

gards.remove(min(gards))

finally you calculate the average of the array :

average = sum(gards) / 4

your solution is good you can overwrite the sum on every input by adding then new value then get the average. but the problem is that you will never know which one is the lowest once the loop is over.

  • Related