0

I am a total beginner here, not familiar with any type of code at all and I thought python would be a good start.

So when I try commenting multiple lines I get a syntax error.

I added in the code, please help me and forgive me for obvious mistakes. I apologise for my action.

print ("no comment")
'''
print ("this is a comment")
print ("so is this")
'''    
print (" not a comment")
5
  • 3
    That's not commenting. Commented Oct 21, 2013 at 17:28
  • 1
    Are you sure? I tried running your code (after removing the indents), and it ran fine. Try posting the rest of your code, and the specific error you have. (You can add info to your post by pressing "edit" in the lower-left of the question) Commented Oct 21, 2013 at 17:28
  • 2
    @kindall It is effectively a comment. (See reference) Commented Oct 21, 2013 at 17:30
  • Please show actual code that causes the error, and the actual error with traceback instead of just a description of it. Commented Oct 21, 2013 at 17:46
  • Depending on the context, such a "comment" may be treated as a docstring. Take some time to learn how to use your text editor to comment out multiple lines at once. Commented Oct 21, 2013 at 18:01

7 Answers 7

3

I'm python doing the ''' you are specifying a long string that can span across many many lines and return the value that you placed in the ''' for example

print(""" Hello

 World
 how are you""") 

Would be printed out like so

 Hello

 World
 how are you 

Comments are done with hashtags for example if you wanted to make a comment in your code you would do print('hello world') #This will print hello world to the console would comment out the part of the code that says #This will print hello world to the console and it will still run your print('hello world') code just would not print out the other lines that you have commented out.

Unfortunately, there is no way to comment out several lines of code (like in HTML) at a time unless you're are using the IDLE or another good python editor like Sublime text to do mass comments if you are in the IDLE you can press Alt-3 on your keyboard to comment out the region to uncomment that region you do Alt-4 but if your in Sublime text you do Control+/ or if your on a MAC you can do Command+/ and that will comment out and/or uncomment out that region.

I hope this made things a bit more easy for you! I wish you the best with your future in python. You will find that it is a very versatile language!

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem does not exist in your code, but possibly in your interpreter.

The interpreter is the system that runs your code (interprets it) and decides how to make sense of the functions you write into it.

However, there is a big change between Python Interpreter 2.7.x and Python 3.x, where the syntax for print used to be

`print "here is your text"`

And now it is

`print("here is your text")`

The reasons for this are a tad ambiguous, but in short print has become a function. These are a very simple concept and are very important to master. But hopefully my explanation has helped you understand.

Also, when other people here talk about comments, multi-line comments, and docstrings, all you need to know is that comments work for one line, multi-line comments or block comments work for many lines, and docstrings are for proper developers, and is a standard way to document your code to make it easier for others to understand and work on.

At this phase you shouldn't worry about it when someone talks about docstrings.

Comments

0

Whatever it is, first of all its not a simple commenting, but while developement people use

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

or

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

but here nothing is wrong to raise an SyntaxError

first check your Python Interpreter version, and do accordingly

Comments

0

I am with @iCodez on IndentationError.

According to this page: http://docs.python.org/release/2.5.1/ref/indentation.html The first line of code cannot be indented.

I ran the code on ideone for checking and it gives error: http://ideone.com/YTuqaG

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

Try removing the leading spaces/ tabs from each line.

1 Comment

Well, I originally thought that. But then he said it was a SyntaxError, so I deleted my comment. I bet you though it is an IndentError and the OP meant "syntax error" as in "error", not the literal SyntaxError.
0

What you have posted does not generate a syntax error, but do be careful with spaces. This generates IndentationError: unexpected indent:

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

And this generates the same error because of the uniform indent:

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

But this has no error at all:

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

Comments

0
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"

Maybe you are typing this outside any function and you might be getting something like this:

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

And may be you are getting something like this:

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

Its not an error its perfectly fine.

Comments

0

Comments are not done with ''' or """ that is a way to have a string go across multiple lines how you comment is by doing # for example

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

If you have multiple lines to comment out what you would have to do is use your IDE highlight the text and dependent on your keyboard or computer you press CONTROL+/ or COMMAND+/ I know that works for sublime text 2 but I am not sure about python's default IDLE

Good luck with your future coding !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.