4. Average

its asking me : Write a function named average() that has two parameters named num1 and num2 .

The function should return the average of these two numbers.

My answer

Write your average function here:

def average(num1, num2):
return num1 + num2 / 2

Uncomment these function calls to test your average function:

print(average(1, 100))

The average of 1 and 100 is 50.5

print(average(1, -1))

The average of 1 and -1 is 0

when I run I get 51.0 and 0.5


The solution is

Write your average function here:

def average(num1, num2):
return (num1 + num2) / 2

Uncomment these function calls to test your average function:

print(average(1, 100))

The average of 1 and 100 is 50.5

print(average(1, -1))

The average of 1 and -1 is 0


On the Return function the only difference from my answer and the correct answer is the parenthesis.

wrong
return num1 + num2 / 2
correct
return (num1 + num2) / 2

I guess my question is why would i get a different average just because of the parenthesis

Hello, @course0691603960, and welcome to the forums.

What does it seem like the difference is? The ‘wrong’ code isn’t returning an average. Python (as well as every programming language I’m familiar with) follows the order of operations.

1 Like

got it thanks forgot about PEMDAS i knew the answer was something simple thanks!!!

1 Like