ike
ike

Reputation: 11

Need help as a beginner with function def

I am new trying to learn to program. I downloaded Python 3.9.5 and trying a function as follows:

>>> def f(x):
    return x**3
f(3)
SyntaxError: invalid syntax

What am I doing wrong or do I need to download something more?

Upvotes: 0

Views: 82

Answers (2)

Azuuu
Azuuu

Reputation: 894

Once you hit the enter after def f(x):, add 4 space indentation. And then hit enter twice until you see another >>> prompt.

>>> def f(x):
...     return x**3
... 
>>> f(3)
27

Upvotes: 2

Have a nice day
Have a nice day

Reputation: 1015

You need to add a a newline:

def f(x): return x**3 
f(3)

Then it is correct syntax.

Upvotes: 2

Related Questions