1

I've very new to programming so sorry if this is a stupid question, but I'm trying to make a program with multiple functions, but whenever I attempt to define one it comes up with an error.

def startUp():
    promptName()

def promptName():
    name = input("Hello. Please enter your name: ")

startUp()
SyntaxError: invalid syntax

If it helps the def part in def promptName(): is highlighted red.

10
  • def means "define." So before you define what a function promptName is, your program has no idea what to do with the string, "promptName()". Commented Aug 2, 2016 at 0:20
  • @Christian I guess it should compile just fine. Are you using the IDLE? Commented Aug 2, 2016 at 0:22
  • 1
    @AndrewCheong: No. What you said matters only when actually running the function -- not while defining it. Also, this is a SyntaxError. OP probably needs to show the entire file. (EDIT: Not entire file, file with MVCE) Commented Aug 2, 2016 at 0:22
  • The "Syntax Error" message should have told you where the error is, at which line. Please include the complete error output, in full and unedited. Also make sure you copy-pasted the actual code and didn't rewrite it in the question (thereby possibly fixing errors in your actual code). Commented Aug 2, 2016 at 0:24
  • 1
    Like everybody are saying, your program (as you show it) is fine. The error is with something you don't show us. Commented Aug 2, 2016 at 0:31

2 Answers 2

5

I'd bet you're trying to paste the entire thing into a Python interpreter session. The command line interpreter needs things entered one block at a time, so try pasting the startUp function, hit enter, then promptName and enter, and then run the whole thing with the last line.

Alternatively, save it all as a .py file and run the file.

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

7 Comments

If I run it as a .py it just opens and closes very quickly, but it works if I enter it into the interpreter separately?
If you use the IDLE, make a new file within it. Then type in your code and run it with f5
I mean an actual file, not just the shell. In that case a syntax error should not pop up.
That's what I'm doing, but I get a box popup just saying called Syntax Error which just says invalid syntax.
Should I just reinstall python?
|
1

The code you posted here is absolutely fine (regarding the syntax). Please check whether you have forgotton a colon or so in you original code.

Regarding the code: If you define a variable (like name within promptName()) within a function, you cannot access that variable from outside the function. To make use of it, you have to return it or state it explicitly as global variable.

4 Comments

I've stripped it down to what I put in the description so that is the entire file and it still won't work. Should I reinstall Python?
@RafaelAlbert Should this not be a comment?
It's the best answer he can give at this time, with some suggestions that might work.
I believe so, I've triple checked it online and it checks out.

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.