-2

I have a simple program that I'm trying to learn in python. I have defined the function as such

>>> def area(base, height):
        return base * height /2

when I try to use the function as such

>>> area(3,4)

I am given this error saying that it is not defined.

Traceback (most recent call last):
 File "<pyshell#3>", line 1, in <module>
area(3,4)
NameError: name 'area' is not defined

Am I missing a vital step?

Kevin
  • 1
  • 1
    it seem you started working in new shell, better to use shell where function is declared – sahasrara62 Sep 22 '21 at 00:17
  • 1
    `File "", line 1, in ` seems to imply you are using a brand new REPL... – juanpa.arrivillaga Sep 22 '21 at 00:23
  • Even when I type area(3,4) directly underneath it, it gives me the same error. Is there a setting in idle that is created a new shell between lines? – Kevin Sep 22 '21 at 00:25
  • Hard to believe. Might be one of the rare cases where a screenshot could help, letting us see what you're really doing. – no comment Sep 22 '21 at 00:41
  • @Kevin after entering the line `return base * height /2` press return *twice*. 1st to finish the line itself and 2nd to finish the function. Then try to call `area(3,4)` again. Does that solve your issue? – molshape Sep 22 '21 at 00:43
  • Here is a screenshot of the error https://ibb.co/bB439r7 – Kevin Sep 22 '21 at 00:46
  • @Kevin Try pressing enter *twice* after your function definition. Your function `area` has not been "finalized", if that makes sense to you. This will solve your issue. – molshape Sep 22 '21 at 00:52
  • Your screenshot shows that when you typed area(3,4) directly underneath it, you did **not** get the same error but a **syntax** error. – no comment Sep 22 '21 at 01:14

1 Answers1

0

Being able to restart IDLE's shell is a feature. It clears everything without having to exit python and restart. It's main use is automatic, when you run a file from the editor. (The restart can be prevented with Run Custom ....)

Having to end interactive compound statements with a blank line is not IDLE specific. The following is from interactive python. IDLE imitates this, for the same reason.

>>> def area():
...     return 3
... area()
  File "<stdin>", line 3
    area()
    ^^^^
SyntaxError: invalid syntax
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52