# Incorrect output using the function

I’m trying to run the simple function in #python.
But the result is not what it should be.
I’ve tried changing the variables and resizing the integer numbers but no help.
The output i’m trying to get is 336 i.e sum of all the integers in the array
but the output is coming to be 54 only.

Code:

‘’’
def calculating_marks(marks):
total=0
for i in marks:
total=total+i

students_marks=[54,43,65,33,65,33,43]
total_marks=calculating_marks(students_marks)
print("Total marks of the student is ",total_marks)
‘’’

I think you have a couple of errors in your code.
This is what I think you need:

``````def calculating_marks(marks):
total=0
for i in range(len(marks)):
total=total+marks[i]

students_marks=[54,43,65,33,65,33,43]
total_marks=calculating_marks(students_marks)
print("Total marks of the student is ",total_marks)
``````

or more simply:

``````def calculating_marks(marks):
return sum(marks)
``````

Hope this helps!

2 Likes

IMHO, It’s much better to use code block by using three backtick(```python) to communicate with others in correct way. One of factors that make Python damn good to read is it’s indentation. Your posted code is hard to read if not impossible to understand…

``````def calculating_marks(marks):
total=0
for i in marks:
total=total+i

students_marks=[54,43,65,33,65,33,43]
total_marks=calculating_marks(students_marks)
print("Total marks of the student is ",total_marks)
``````

returns `Total marks of the student is 54`

``````def calculating_marks(marks):
total=0
for i in marks:
total=total+i

students_marks=[54,43,65,33,65,33,43]
total_marks=calculating_marks(students_marks)
print("Total marks of the student is ",total_marks)
``````

returns `Total marks of the student is 336`

Indentation matters I think.

4 Likes

@capymind Fully agree! - I should have realised it was just an indentation problem. Yours is a much more elegant solution than my proposal.

1 Like

``````def calculating_marks(marks):
return sum(marks)
``````

is awesome for me. I think
everybody says your suggestion is more pythonic.

Python is high, high, high… - level language.
(If not, I go to other languages such as Rust, I give up programming itself.)