-1

I'm stumped. I looked through my code a bunch of times and can't find out why I'm getting an invalid syntax for this bit of code. Any help would be greatly appreciated! Thanks.

def nameReverse():

    name = str(input("Enter your full name: "))
    testName = name.split()

    if len(testName)>1:

    firstName=testName[0]
    lastName=testName[1]
    print (lastName,firstName)

def main():
    nameReverse()

main()
4
  • 1
    It seems you didn't intend 'nameReverse()' correctly. Commented Oct 9, 2017 at 13:14
  • nameReverse() is not correct and also always show the error that you are getting :) Commented Oct 9, 2017 at 13:15
  • 1
    The actual stack trace would be quite useful. This error is due to wrong indentation or mixing spaces and tabs. Commented Oct 9, 2017 at 13:16
  • @farbiondriven it's indented one tab in my shell, I don't know why it didn't copy over. Thanks for the reply! Commented Oct 9, 2017 at 13:17

2 Answers 2

1

If it is python 2.x you should use

name = str(raw_input("Enter your full name: "))

Full code:

def nameReverse():

    name = str(raw_input("Enter your full name: "))
    testName = name.split()

    if len(testName)>1:

        firstName=testName[0]
        lastName=testName[1]
        print (lastName,firstName)

def main():
    nameReverse()

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

Comments

1

The only error I got was an indentation error. This is python, so indentation is critical. Your if statement was improperly indented. Here is what you want:

def nameReverse():
    name = str(input("Enter your full name: "))
    testName = name.split()

    if len(testName)>1:
        firstName=testName[0]
        lastName=testName[1]
        print (lastName,firstName)

def main():
    nameReverse()

main()

5 Comments

Thanks for the reply! I copied this code into Python and am still getting a syntax error at 'def main():'
SyntaxError: invalid syntax, the highlighted part is the 'def' before 'main'
So, you copied this code into a python file and ran it with python my_file.py and it didn't work?
I got it to work, thanks for the help! This is my first-semester coding so I'm still getting used to all of the rules and constants.
What was the problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.