Skip to main content
 首页 » 编程设计

python之python中的函数问题

2025年05月04日17小虾米

我是python新手。我正在尝试编写一个非常基本的功能,但出现错误。我不知道背后的原因。示例代码:

def printme( str ): 
    print str; 
    return; 
 
printme("My string"); 

这应该按逻辑执行,但它给了我以下错误:

追溯(最近的调用最后): 文件“标准输入”,第 1 行,在“模块”
NameError: 名称 'printme' 未定义

欢迎提出任何建议...

请您参考如下方法:

分号不应该像 return 语句一样存在(函数的执行在其中缩进的最后一个语句处结束)。

不完全确定你是如何格式化缩进的,但 python 依赖它来确定范围

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 

当前执行

python syntax 上的维基百科页面涵盖缩进规则。