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()
-
1It seems you didn't intend 'nameReverse()' correctly.farbiondriven– farbiondriven2017年10月09日 13:14:52 +00:00Commented Oct 9, 2017 at 13:14
-
nameReverse() is not correct and also always show the error that you are getting :)Kalariya_M– Kalariya_M2017年10月09日 13:15:53 +00:00Commented Oct 9, 2017 at 13:15
-
1The actual stack trace would be quite useful. This error is due to wrong indentation or mixing spaces and tabs.Laur Ivan– Laur Ivan2017年10月09日 13:16:14 +00:00Commented 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!JarPaJack– JarPaJack2017年10月09日 13:17:49 +00:00Commented Oct 9, 2017 at 13:17
2 Answers 2
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()
answered Oct 9, 2017 at 13:29
farbiondriven
2,4782 gold badges21 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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()
answered Oct 9, 2017 at 13:29
Steampunkery
3,8742 gold badges23 silver badges28 bronze badges
5 Comments
JarPaJack
Thanks for the reply! I copied this code into Python and am still getting a syntax error at 'def main():'
JarPaJack
SyntaxError: invalid syntax, the highlighted part is the 'def' before 'main'
Steampunkery
So, you copied this code into a python file and ran it with
python my_file.py and it didn't work?JarPaJack
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.
Steampunkery
What was the problem?
lang-py