Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect output when averaging in python

Tags:

python

There is no error appearing in this python code but the output is incorrect. This is a code about calculating the average marks of a student.

english_1 = 22
urdu_1 = 23
maths_1 = 15
science_1 = 18
social_1 = 21

english_2 = 10
urdu_2 = 22
maths_2 = 13
science_2 = 25
social_2 = 11
def average_marks(english, urdu, maths, science, social):
    average = english + urdu + maths + science + social / 5
    print("average marks of student")
    print(average)
result1 = average_marks(english_1, urdu_1, maths_1, science_1, social_1)
result2 = average_marks(english_2, urdu_2, maths_2, science_2, social_2)

this is the output

average marks of student 82.2 average marks of student 72.2

If you can pls help i am a beginner. Thank You!

like image 737
AbubakrShaik Avatar asked Aug 27 '26 01:08

AbubakrShaik


2 Answers

You are missing parentheses when calculating the average. You want to make the sum first and then make the division:

average = (english + urdu + maths + science + social) / 5

Hopefully I was of any help

like image 96
Eduardo Barrancos Avatar answered Aug 29 '26 15:08

Eduardo Barrancos


Your code looks good to me but one thing Try setting result1 to a double or a float instead of a int therefore when python writes the result it will not encounter any restrictions and add parentheses around lie this

(english + math + science) / # of items
like image 38
Joseph Attia Avatar answered Aug 29 '26 14:08

Joseph Attia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!