Reputation: 291
I am a newbie in python. I am trying to code a very basic function but getting errors. I dont know the reason behind it. Example code:
def printme( str ):
print str;
return;
printme("My string");
This should execute logically but it gives me the following error:
Traceback (most recent call last):
File "stdin", line 1, in "module"
NameError: name 'printme' is not defined
Any suggestions are welcome...
Upvotes: 1
Views: 2708
Reputation: 9005
It does not work because of your indentation error. Your function never compiled so it does not exists.
(Original question has been edited away for 'proper formatting')
Upvotes: 2
Reputation: 1155
It might help to follow the python style guide (pep8). You don't have to, but it will help avoid your indentation errors, and it will be easy to read other peoples code.
Upvotes: 2
Reputation: 2872
The semicolons shouldn't be there as well as the return statement (the execution of the function ends at the last statement indented within it).
Not entirely sure how you formated the indent but python relies on that to determine scope
def printme(str):
print str #This line is indented,
#that shows python it is an instruction in printme
printme("My string") #This line is not indented.
#printme's definition ends before this
executes currectly
wikipedia's page on python syntax covers the indentation rules.
Upvotes: 5