Alina Alina - 1 month ago 5
Python Question

How do I change my code to use arrays?

This is my code so far. I can get my code to prompt the user to input 4 test scores. I have made it so it asks "Enter the score for test number N:"

My problem lies in being able to extract the input values from what the user would input. My end goal is to be able to drop the lowest test grade and calculate an average with the remaining scores. Input data used would be 99, 99, 99 and 77.

I have an example that takes the min from a variable that is assigned X numbers but not from when you would get it from a user input.

def main():
scores = getNumbers()

def getNumbers():
testCountIs = 0
testNum = 4
totalScore = 0

for test in range(1, testNum+1):
print('Enter the score for test number', int(testCountIs+1), end='')
testScores = int(input(': '))
testCountIs += 1

main()


Edit2: below is the code that works for this program.

def main():
scores = getNumbers()
print("The average with the lowest score dropped is: ",float(sum(scores))/len(scores))

def getNumbers():
scores=[]
for testNum in range(4):
print('Enter the score for test number',int(testNum+1),end='')
scores.append(int(input(': ')))
scores=[score for score in scores if min(scores)!=score]
return scores

main()

Answer

This is what you want:

def getNumbers():
   scores=[]
   for testNum in range(4):
       print('Enter the score for test number %d'%(testNum+1)),
       scores.append(input(': '))
   scores=[score for score in scores if min(scores)!=score]
   return scores

scores= getNumbers()
average= float(sum(scores))/len(scores)
print average

Edit For python3

def getNumbers():
    scores=[]
    for testNum in range(4):
       print('Enter the score for test number',int(testNum+1),end='')
       scores.append(int(input(': ')))
    scores=[score for score in scores if min(scores)!=score]
    return scores

scores= getNumbers()
print(float(sum(scores))/len(scores))