Python Forum
[split] SyntaxError: invalid syntax - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [split] SyntaxError: invalid syntax (/thread-8442.html)



[split] SyntaxError: invalid syntax - arth4 - Feb-20-2018

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


RE: [split] SyntaxError: invalid syntax - Larz60+ - Feb-20-2018

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
>>>



RE: [split] SyntaxError: invalid syntax - snippsat - Feb-20-2018

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]


RE: [split] SyntaxError: invalid syntax - arth4 - Feb-21-2018

Thanks for the help.
I executed the code block the way it was told and got correct results