Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (anonymous):

I am working on the "while" function in Lecture 2. When I type "print y" (without the quotes), I get a syntax error. Does anyone know why?

OpenStudy (owlfred):

Hoot! You just asked your first question! Hang tight while I find people to answer it for you. You can thank people who give you good answers by clicking the 'Good Answer' button on the right!

OpenStudy (anonymous):

can you please put the rest of the while loop and I'll try to answer

OpenStudy (anonymous):

>>> y=0 >>> x=3 >>> itersLeft = x >>> while(itersLeft>0): y=y+x itersLeft=itersLeft-1 print y SyntaxError: invalid syntax

OpenStudy (anonymous):

I think because you haven't indented it correctly. the line y = y + x should be tabbed in also the next one

OpenStudy (anonymous):

Hmm. on my screen in Python, those two lines are indented, but after copying them into the chat box, they are no longer indented...

OpenStudy (anonymous):

I'm not sure, when I run it, it comes up with 9

OpenStudy (anonymous):

That's strange. I guess I'll just keep trying. Thanks for your help Joz!

OpenStudy (anonymous):

are you sure they are not in line with while? I'm just saying, because the print y is further left on the chat box

OpenStudy (anonymous):

in my python interface, "y=y+x" and "itersLeft = itersLeft - 1" are both indented to the right of the "while" statement. "print y" is to the left of the while statement

OpenStudy (anonymous):

yeah, that sounds right to me. When it comes up with the error, is there a pink thing on the script part?

OpenStudy (anonymous):

This is basically my first time programming, so I'm not exactly sure what you mean by script. "print" is highlighted in pink, and below that it says "SyntaxError: invalid syntax"

OpenStudy (anonymous):

hmmm... try lining it all on the left hand side, except the 2 lines under while. Failing that, I don't know.

OpenStudy (anonymous):

ok i'll give that a shot. my last question for you - how do i line everything up on the left side? every type i start a line, it starts with ">>>"

OpenStudy (anonymous):

wait... are you doing it straight into the shell?

OpenStudy (anonymous):

try to find the IDLE page. give me a minute and I'll tell you the path to find it...

OpenStudy (anonymous):

well, i feel like an idiot. As I was watching the video lecture, I noticed my screen looked different, but I ignored it. You're right, the top of my screen says "Python Shell"

OpenStudy (anonymous):

is it a white screen, yeah? (Don't worry, I did the same)

OpenStudy (anonymous):

just go to file, new window. If you don't have an option bar on the top, let me know

OpenStudy (anonymous):

ahh - that gave me a blank screen. i typed in the code, saved, and clicked "run module" and that gave me the right output for y (9). Thank you so much for walking me through this

OpenStudy (anonymous):

Haha no worries, you are set up now...

OpenStudy (anonymous):

also make sure when you save, you save as name.py

OpenStudy (anonymous):

ok got it. thanks again...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!