Python:未知的无效语法错误

时间:2017-10-09 13:13:09

标签: python syntax syntax-error

我很难过。我仔细查看了我的代码,但却无法找出为什么我的代码有了无效的语法。任何帮助将不胜感激!感谢。

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

2 个答案:

答案 0 :(得分:1)

如果是python 2.x,你应该使用

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

完整代码:

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

答案 1 :(得分:1)

我得到的唯一错误是缩进错误。这是python,所以缩进很关键。您的if语句缩进不正确。这是你想要的:

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