Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function on the command line not working

Consider this simple python:

def cube(x): 
   return x*x*x;

if __name__ == '__main__':
    print(cube(4));

Works ok. But when I open up the python command line interpreter and do:

>>> def cube(x): return x*x*x;
... cube(4);

I get:

File "<stdin>", line 2
    cube(4);
     ^
SyntaxEror: invalid syntax

What stupid thing are my doing wrong?

like image 471
dublintech Avatar asked Dec 27 '22 15:12

dublintech


1 Answers

Try pressing enter one more time :) The ellipse in front of your cube(4) indicates that you are still defining your function. Also, you can remove the semicolon:

>>> def cube(x): return x*x*x
...
>>> cube(4)
64
like image 56
RocketDonkey Avatar answered Dec 31 '22 14:12

RocketDonkey