0

I am trying to define a function in python but I am getting an error, here is the code,

def func greet_user():
"""Display a simple greeting."""
    print("Hello!")
    greet_user()

Then I get the error, "invalid syntax" . Any help would be appreciated.

4
  • 2
    def func is not valid; just write def. And strings are delimited with either one quote character, or three - you can't use two. Commented Feb 27, 2020 at 23:57
  • You should probably read this first. Commented Feb 27, 2020 at 23:59
  • Post the entire message including the stack trace. It shows us the line with the failure. Commented Feb 28, 2020 at 0:08
  • It just said Syntax invalid" @tdelany But I understand now. Commented Feb 28, 2020 at 0:10

4 Answers 4

2

Python enforces strict indentation, all the content of a python function, including the comments, needs to be equally indented. To fix your case, you should:

1) remove "func" from 'def func greet_user():',

2) tab the docstrings,

3) untab when you call the function outside of the function.

Also docstrings needs three quotation marks """string""" not ""string ""

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

2 Comments

It still says invalid syntax.
The string needs """ not "" to be valid
1

After you fix indentation you need to wrap ""display a simple greeting"" in triple quotes.
Then change def func greet_user(): to def greet_user():

Comments

1

Try this:

def greet_user():
    print("""Display a simple greeting.""")
    print("Hello!")

greet_user()

Output:

Display a simple greeting.

Hello!

1 Comment

'Display a simple greeting' was probably a docstring, not part of the intended output. I'd suggest not printing it, as it changes the author's intent.
1

Remove func and move greet_user() out of the function (unindent)

def greet_user(): #display a simple greeting
    print("Hello!")

greet_user() #prints 'Hello!'

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.