Why do not this code work correctly?


#1

I wrote this code to return sun of grades, but it returns 78, I do not know why, wrongly.

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(grades):
total=0
for i in range(len(grades)):
total+=i
return total

print grades_sum(grades)

What is the problem?


#2

Do you indent this line to the same indent level as your for loop?


#3

No. In the real program the indent level of them is not same and I noticed that, but I am finding error.


#4

please post your preformatted code by highlighting your code and then clicking this button </>


#5

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(grades):
   total=0
   for i in range(len(grades)):
      total+=i
   return total

print grades_sum(grades)

This is the program.


#6

i am facing similar problem in my code. it works fine in the python compilation but codeacademy throws error

n = [3, 5, 7]

def total(numbers):
result = 0
i = 0
for i in range(len(n)):
print n[i]
result = result + n[i]
# print result
return result
Error as
" OOPs try again total[0,3,6] returns 15 instead of 9

help me out to resolve this


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.