-1
l= float(input("Enter length: "))
w= float(input("Enter width: "))
r= float(input("Enter radius: "))
def perimeter():
    return (l+w)*2
def circArea():
    return (3.14)*(r**2)
def display():
    p = perimeter()
   print("Perimeter is: ", p)
    a = circArea()
    print("Area is: ", a)
def main():

    display()
main()

I fixed the code, it works now. I realized what i was doing wrong with the returns.

Jefa
  • 45
  • 1
  • 1
  • 8
  • 2
    return(p) will make the function return and not execute c=... And you defined c/p in the context of main, not in the context of the prints... c,p=main() and return(c,p) at the end of the def should be enough – Fábio Dias Oct 19 '15 at 01:49
  • 5
    You really, really need to read the [official Python tutorial](https://docs.python.org/3.4/tutorial/index.html). – TigerhawkT3 Oct 19 '15 at 01:49
  • Once you use `return` in your `main` function it will stop executing the rest of the code, so it never gets to `c`. Also `c` and `p` are declared OUTSIDE your function, so why would you expect Python to know what they are? – Green Cell Oct 19 '15 at 01:51
  • @GreenCall: Where, exactly, are `c` and `p` *declared*? And if they were *declared* outside the function, then there would be no name errors where they are being used. – Scott Hunter Oct 19 '15 at 01:53
  • @ScottHunter They are being declared in `main()`, even if they were declared earlier with a default value they still wouldn't magically get the right values being calculated in `main()` – Green Cell Oct 19 '15 at 01:57

1 Answers1

0

As pointed out in the comments, you return from main before you compute c. But even if you didn't, c is local to main (as is p), so it wouldn't be accessible from outside of it. If you want to access the global p and c, you have to tell main that with the global statement. But that's really a bad way to handle the problem of getting data out of a function; that's what return values are for.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101