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 trial

Python Python Basics (Retired) Things That Count Basic Numbers

Rachel Leffel
Rachel Leffel
7,418 Points

I am getting a syntax error when trying to run python string_multiply.py

I have entered all of the information from the video but am getting an error message that says "Syntax Error: Invalid Syntax"

What I entered in the top box:

input_string = input("What string do you want to use? ")
input_int = input("How many times should I repeat it? ")

print(input_string*int(input_int))

What gives?

2 Answers

Kedar Raje
Kedar Raje
228 Points

I think what may be happening is you are running it from the python interpreter..I was doing that and kept getting the same irritating error. You should run it from the normal unix shell

treehouse:~/workspace$
treehouse:~/workspace$ python stringmultipy.py
What string do you want to use? Boy
How many times do you want to print the string?15
BoyBoyBoyBoyBoyBoyBoyBoyBoyBoyBoyBoyBoyBoyBoy
treehouse:~/workspace$

Rachel Leffel
Rachel Leffel
7,418 Points

I think you're right. The syntax error only happens when I enter "python" into the console and then type in my code. The code does work if I only type "python" at the beginning of the function and don't enter it the extra time beforehand.

What I was doing was entering "python" before I tried to run any code, which gave me the syntax error:

treehouse:~/workspace$ python

treehouse:~/workspace$ python string_multiply.py

What I should have been doing instead was not typing in "python" initially:

treehouse:~/workspace$ python string_multiply.py

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Your code seems to have an extra space before the line print(input_string*int(input_int), Python language is very strict about the use of white space and indentation, having an unnecessary space character can cause indentation error.

Rachel Leffel
Rachel Leffel
7,418 Points

Thanks for the response! Unfortunately removing the space does not fix the problem. I actually added the space when I was playing around to see if I could get the code to work.

I am still getting a syntax error :(

William Li
William Li
Courses Plus Student 26,868 Points

No, nothing is wrong with your code aside from that extra space. I copied the code to my Mac and it executed fine using Pyhton3.

Imgur

Rachel Leffel
Rachel Leffel
7,418 Points

This is strange. When I put that exact text into Workspaces I get an error that says:

File "<stdin>", line 1
 python string_multiply.py
                                     ^
SyntaxError: invalid syntax

I guess it's an issue with Workspaces?

William Li
William Li
Courses Plus Student 26,868 Points

Hi, Rachel, yeah, perhaps so, I personally don't use Workspace much, so I'm not entirely sure about that.

Rachel Leffel
Rachel Leffel
7,418 Points

Thank you anyway! I will just move on with the course :)