繁体   English   中英

语法错误:尝试评论时语法无效

[英]syntax error: invalid syntax when trying to comment

我在这里完全是初学者,根本不熟悉任何类型的代码,我认为 python 将是一个好的开始。

因此,当我尝试注释多行时,出现语法错误。

我在代码中添加了,请帮助我并原谅我明显的错误。 我为我的行为道歉。

print ("no comment")
'''
print ("this is a comment")
print ("so is this")
'''    
print (" not a comment")

我是 python 做'''你指定一个长字符串,它可以跨越许多行并返回你放在'''的值

print(""" Hello

 World
 how are you""") 

会像这样打印出来

 Hello

 World
 how are you 

评论是用主题标签完成的,例如,如果你想在你的代码中发表评论,你会做print('hello world') #This will print hello world to the console将注释掉说#This will print hello world to the console的代码部分#This will print hello world to the console ,它仍然会运行你的print('hello world')代码,只是不会打印出你注释掉的其他行。

不幸的是,除非您使用 IDLE 或其他优秀的 Python 编辑器(如 Sublime text)来进行大量注释,否则无法一次注释掉几行代码(例如在 HTML 中),如果您在 IDLE 中,则可以按键盘上的Alt-3注释掉该区域以取消注释该区域,您执行Alt-4但如果您在 Sublime 文本中执行Control+/或者如果您在 MAC 上执行Command+/这将注释掉和/或取消注释出那个地区。

我希望这让你的事情更容易一些! 我祝你在 Python 中的未来一切顺利。 你会发现它是一种非常通用的语言!

您的问题不存在于您的代码中,而可能存在于您的解释器中。

解释器是运行您的代码(解释它)并决定如何理解您写入其中的函数的系统。

然而,Python Interpreter 2.7.x 和 Python 3.x 之间有很大的变化,其中print的语法曾经是

`print "here is your text"`

现在是

`print("here is your text")`

这样做的原因有点模棱两可,但简而言之, print已成为一种功能 这些都是非常简单的概念,掌握起来非常重要。 但希望我的解释能帮助你理解。

另外,当这里的其他人谈论注释、多行注释和文档字符串时,您只需要知道注释适用于一行多行注释块注释适用于行,而文档字符串适用于适当的开发人员,并且是一种标准的方式来记录你的代码,让其他人更容易理解和处理。

在这个阶段,当有人谈论docstrings时,您不应该担心。

def hello():
    """ 
    This is a doc string
    It has some information about what 
    the function does
    """
    print("Hello")

hello() # Calling hello. This is a comment. Prints "Hello"

也许你在任何函数之外输入这个,你可能会得到这样的东西:

print("This is not a comment")
'''
    print('This is a comment')
'''

可能你会得到这样的东西:

'\n\tprint("This is a comment")\n'

这不是错误,完全没问题。

不管它是什么,首先它不是一个简单的评论,而是在开发人员使用

'''text''' or """text"""

'''
line 1
line 2
and so on
'''

但这里引发 SyntaxError 没有错

首先检查您的 Python 解释器版本,然后执行相应操作

我在 IndentationError 上与 @iCodez 在一起。

根据这个页面: http : //docs.python.org/release/2.5.1/ref/indentation.html第一行代码不能缩进。

我在 ideone 上运行代码进行检查,但出现错误: http ://ideone.com/YTuqaG

    print ("no comment")
    '''
    print ("this is a comment")
    print ("so is this")
    '''    
    print (" not a comment")

尝试从每行中删除前导空格/制表符。

您发布的内容不会产生语法错误,但请注意空格。 这会生成IndentationError: unexpected indent

print ("no comment")
 '''
^ note leading space 
print ("this is a comment")
print ("so is this")
'''    
print (" not a comment")

由于统一缩进,这会产生相同的错误:

    print ("no comment")
    '''
    print ("this is a comment")
    print ("so is this")
    '''    
    print (" not a comment")

但这根本没有错误:

print ("no comment")
'''
print ("this is a comment")
print ("so is this")
   '''
#^^ leading spaces    
print (" not a comment")

注释不是用'''""" ,这是一种让字符串跨越多行的方法,例如通过执行#进行注释的方式

#print ("hello world")
#this is a comment
#so is this
#I am a comment
print('I still work')

如果你有多行注释掉你必须做的就是使用你的 IDE 突出显示文本并依赖于你的键盘或计算机你按CONTROL+/COMMAND+/我知道这适用于 sublime text 2 但我不确定 python 的默认空闲

祝你未来的编码好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM