Python Forum

Full Version: [split] SyntaxError: invalid syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm totally new to python programming. I've written a function that will return the square of a number. Below is the code for the same:
numcalls = 0
>>> def square(x):
	global numcalls
	numcalls = numcalls + 1
	return x * x
d = square(5)
However getting the below error:
Error:
SyntaxError: invalid syntax
I've also tried with the following code:
numcalls = 0
>>> def square(x):
	global numcalls
	numcalls = numcalls + 1
	return x * x
print (square(5))
but the same error persist.

Please let me know the exact syntax for both the cases.

Thanks,
Arnab
when pasting in the interactive interrupter, you must do it line by line, making sure
lines are indented properly. works just fine if you do this:
>>> numcalls = 0
>>> def square(x):
...     global numcalls
...     numcalls = numcalls + 1
...     return x * x
...
>>> d = square(5)
>>> d
25
>>>
You did post in another post,should have made a new Thread.

You use interactive shell and i guess IDLE.
Interactive shell is <Enter> after every line or block of code.
To write code and run it do as in image.
Then there is no >>>.
def square(x):
    return x * x

print (square(5))
Output:
25
[Image: Wn7asS.jpg]
Thanks for the help.
I executed the code block the way it was told and got correct results