0

How to add comments in Python code with break-lines? I suppose the following should not work. Is there any wrappers for Pythons that support the break-lines?

# Program make a simple calculator

# This function adds two numbers
def add(x, y):
    return x + y

# This function subtracts two numbers
def subtract(x, y):
    return x - y

# This function multiplies two numbers
def multiply(x, y):
    return x * y

print(subtract(6, \  # = 6 - (3 * (1 + 2))
multiply(3, \  # = 3 * (1 + 2)
add(1, 2))))  # = 1 + 2

1 Answer 1

3

There must not be any characters after \, not even whitespace. Python has implicit line continuation though, so it's possible to spread the expression over multiple lines and add comments like this (though I'm not sure if it makes the code more readable):

print(subtract(6,  # = 6 - (3 * (1 + 2))
               multiply(3,  # = 3 * (1 + 2)
                        add(1, 2)  # = 1 + 2
                       )
              )
     )
Sign up to request clarification or add additional context in comments.

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.