JarPaJack
JarPaJack

Reputation: 9

Python: Unknown Invalid Syntax Error

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()

Upvotes: -1

Views: 5894

Answers (2)

Steampunkery
Steampunkery

Reputation: 3874

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()

Upvotes: 1

farbiondriven
farbiondriven

Reputation: 2468

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()

Upvotes: 1

Related Questions