1

I'm trying to work on a school assignment that asks the user to input 3 integers, then I need to pass these three integers as parameters to a function named avg that will return the average of these three integers as a float value.

Here's what I've come up with so far, but I get this error:

line 13, in <module>
    print (average)
NameError: name 'average' is not defined  

Advice?

    a = float(input("Enter the first number: "))
    b = float(input("Enter the second number: "))
    c = float(input("Enter the third number: "))

    def avg(a,b,c):
        average = (a + b + c)/3.0
        return average


    print ("The average is: ")
    print (average)

    avg()
3
  • "print" is a statement, not a function. You should not use parenthesis around what you want to print. Commented Mar 14, 2014 at 2:22
  • 1
    @jrennie OP did not specify if this was Python 2.x or 3.x, but if this is Python 3, print is indeed a function and requires parentheses Commented Mar 14, 2014 at 13:02
  • @Cyber Doh! Sorry for showing off my ignorance of 3.x Commented Mar 14, 2014 at 20:12

3 Answers 3

1

average only exists as a local variable inside the function avg

def avg(a,b,c):
    average = (a + b + c)/3.0
    return average

answer = avg(a,b,c) # this calls the function and assigns it to answer

print ("The average is: ")
print (answer)
Sign up to request clarification or add additional context in comments.

1 Comment

I can see where I went wrong with not passing the variables. Thanks!
0

You should print(avg(a,b,c)) because the average variable is only stored in the function and cannot be used outside of it.

Comments

0
  1. You called avg without passing variables to it.
  2. You printed average which is only defined inside the avg function.
  3. You called avg after your print.

Change print (average) to

average = avg(a, b, c);
print(average)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.