Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How this code is failing to give me an accurate answer?

this is my code which is working fine but it is not correct. I am unable to figure out the problem

class MathUtils:

    @staticmethod
    def average(a, b):
        return a + b / 2

print(MathUtils.average(2, 1))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you made a tiny mistake – what your code actually does is taking b, dividing it by 2 and then add the result to a. so you get 2 + 0.5 = 2.5

you need to put parentheses around a + b:

class MathUtils:

@staticmethod
def average(a, b):
    return (a + b) / 2

print(MathUtils.average(2, 1))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading