Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAbdullah Jassim
4,551 PointsI am getting an invalid syntax error
Step 1:
Make two strings, each should be 8 characters long, made up of Xs and Os.
First string should start with X, second string should start with O.
Both strings should alternate between the two characters.
You can use multiplication for this.
A = 'XO' * 4 B = 'OX' * 4
Step 2:
Make a list
Add 1 of the X-started strings.
Add 1 of the O-started strings.
Repeat until you have 8 items total in the list.
You can use multiplication for this, too.
C = [A,B] * 4
Step 3:
Print out the list of strings, joined with newlines \n.
print("\n".join(C))
then when i run - python string_manip.py in the console below, I get an invalid syntax error. Can you please advise?
4 Answers
Noah Fields
13,985 Points'''python A = 'XO' * 4 B = 'OX' * 4 '''
Are these on the same line of code? If so, that's why it isn't working. Try changing it to:
'''python A = 'XO' * 4 B = 'OX' * 4 '''
Some languages can do two commands on one line, though it's generally considered bad practice to do so because it's hard to read. These languages will look at your code and the characters in it, "compile" themselves into something easier for your computer to understand, and run. However, because of the way Python works, it goes one line at a time instead of compiling the whole thing at once. Because of this, line spacing is very important. If two commands are on the same line, Python can't do both at once and will give you an error code - as seen above.
At least, I believe this is how that works. I could be wrong.
Jacob Brooks
Python Web Development Techdegree Student 1,692 PointsI had the same issue the first time I ran the code. I had used the console to work through the process and tried to run the finalized code and it returned the invalid syntax message. I quit or exited the console and entered the command again (python string_manip.py) at the initial workspace$ startup command line and it worked. I guess to run your code you have to run it from that initial command line. Thanks.
sarah070707
315 PointsI ran his exact same code and got a syntax error
it does not like xo = 'xo' + 4
Jacob Brooks
Python Web Development Techdegree Student 1,692 PointsJanessa, you have a + (add) instead of a * (multiplied).
xo = 'xo' * 4
sarah070707
315 PointsOh, my goodness thank you. Didn't even realize
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsChris Howell
Python Web Development Techdegree Graduate 49,702 PointsCould you post a copy/paste of your code snippet causing the error.
Make sure you use the Markdown Cheatsheet to properly format a Code snippet so it shows up correctly within this forum page.