I'm starting Comp Sci courses in Uni this coming fall (starting with zero programming knowledge), so I'm just starting to play around programming. I'm following a book and tried copy-pasting some code - but it doesn't work. Here's what I tried:
>>> def function(x):
return x+2
function(2)
SyntaxError: invalid syntax
The word "function" was highlighted. I'm confused because the very same example is used in the book and it appears to work but then I get that error on my end. What's going on here?
-
1Good luck with your efforts! Knowing where to ask for help is a good start for anything, and Stack Overflow has lots of help available. I recommend reading existing questions to see how other people are using the language and to get a sense for how Python code looks.Brighid McDonnell– Brighid McDonnell2012年08月10日 01:12:18 +00:00Commented Aug 10, 2012 at 1:12
-
I'll recommend here using mostly the internet to teach yourself programming. Go for docs.python.org and work through the tutorial there. It's how I learned, and I suspect many others who frequent this site.yurisich– yurisich2012年10月04日 02:27:11 +00:00Commented Oct 4, 2012 at 2:27
4 Answers 4
You need to separate the function definition from its execution. Also, Python is sensitive to whitespace at the beginning of lines. Try this (exactly):
def function(x):
return x+2
function(2)
or, in one line (which you should not do; see the style guidelines):
def function(x): return x+2; function(2)
or, in the Python shell:
>>> def function(x):
return x+2
>>> function(2)
4
Note the blank line between the function definition and its use. After you define the function, hit enter once to get the prompt back.
Comments
I'm assuming you meant to put Python in the title. Python has interesting syntax rules in that it actually counts white space as meaningful when parsing the program. What I mean is that having extra space, newlines, or tabs, etc. actually changes the meaning of the program. Double check the book example and make sure you have the exact same (tabs, new lines, and all) syntax written. It may look closer to this:
def f(x):
return x + 2
note the new line and tab. To call this function, on a separate line say:
f(5)
or replace 5 with whatever parameter you want.
edit:
so the full script should be:
def f(x):
return x + 2
f(2)
1 Comment
Try this:
def function(x):
return x+2
function(5)
In python, indentations are important. They are the {} of the python world.
You actually do not need to add extra whitespace before function(5) because python knows not to include it in the function definition because it is not indented. It is still good practice to add the extra blank line, but it is not strictly necessary.
1 Comment
>>> in the question indicates that the asker is executing statements in a REPL. In this case, a blank line is necessary to end the previous statement and begin a new statement before using the function.This is for the users using Python 2.6.6 and IDLE 2.6.6.
Since Python interpreter is very much sensitive to whitespace and indentations, we need to separate the function declaration from the execution.
What you must be doing:
>>> def square(input):
output=input*input
return output
print square(5)
Output: SyntaxError: invalid syntax
Correct way to do it:
>>> def square(input):
output=input*input
return output
>>> print square(3)
9