开发者

Problem with function in python

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


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.


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.


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


try cp this:

def printme(str):
    print str

printme("My string")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜