1

I am trying to add each row of nested loop independently in order to find averages. I am missing a detail which may or may not derail my whole code.The code should compute the average of a given row of scores but drops the lowest grade.

def printAverages():
scores = [[100,100,1,100,100],
          [20,50,60,10,30],
          [0,10,10,0,10],
          [0,100,50,20,60]]
total = 0
minValue = 100
counter = -1
for row in scores:
    counter = counter + 1
    for n in scores[0]:
        total = total+n
        if minValue > n:
            minValue = n
    total = total - minValue
    print("Average for row",counter,"is",total)
    total = 0

How do i make it so for n in score [0] takes the average of each row instead of only computing the average of the first row? I know that scores[0] commands the program to only read the first row, I just don't know how to change it.

Thank you

1
  • for n in row instead of for n in scores[0] is what you're looking for I think. Commented Feb 21, 2016 at 22:31

2 Answers 2

1

Remember, the XY problem.

# Sum of a list of numbers divided by the number of numbers in the list
def average(numbers):
    return sum(numbers)/len(numbers)

# This is your data
scores = [[100,100,1,100,100],
          [20,50,60,10,30],
          [0,10,10,0,10],
          [0,100,50,20,60]]

# enumerate() allows you to write FOR loops naming both the list element and its index
for (i, row) in enumerate(scores):
    print("Average for row ", i, "is ", average(row))

Keep in mind that Python supports functional programming and encourages the programmer to write pure functions when possible!

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

5 Comments

Thank you for your answer! I'm currently taking a introductory python class and I have very little knowledge of all the functions available to me in python. I was wondering also, if the program asks for "no user inputs", does that mean the scores would have to be manually input into the code itself?
Yes, but you have a third option, which can be quite interesting: generating random numbers! The build-in random library will be helpful (you can read about it here)
By the way, you will definitely enjoy this read if you are new to Python.
Thanks for all the info! Most the assignments we have been doing called for us to code without using built in functions. For example doing a min and max function with if statements. I wasn't about this assignment so I just tried to write everything out in a forloop as confusing as it may be.
You're welcome! Well, that's a great way to learn syntax and logic components. It will be like heaven for you once you will be able to use all the built-ins.
0

the statement for n in scores[0]: will only go through the first column.

What you want it to say is for n in row:. That will have it go through each row, one row per loop of the outer loop

1 Comment

Thank you! This is what I was looking for.

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.