anonymous
  • anonymous
On week 2 beginning 'Iteration', I get an error on the print statement see below http://pastebin.com/raw.php?i=sFhLznUw Any one know why ?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
i don't get an error what did the traceback say?
MicroBot
  • MicroBot
@minimallinux check your spelling. Python is case sensitive:P
anonymous
  • anonymous
File "", line 4 print (str(x) + '*' + str(x) + '=' + str(ans)) ^ SyntaxError: invalid syntax >>>

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

MicroBot
  • MicroBot
itersleft = itersLeft - 1 << wrong spelling
anonymous
  • anonymous
Fixed that still error x = 3 >>> ans = 0 >>> itersLeft = x >>> while (itersLeft != 0): ... ans = ans + x ... itersLeft = itersLeft - 1 ... print (str(x) + '*' + str(x) + '=' + str(ans)) File "", line 4 print (str(x) + '*' + str(x) + '=' + str(ans))
MicroBot
  • MicroBot
print (str(x) + '*' + str(x) + '=' + str(ans)) >>>>> print(str(x) + '*' + str(x) + '=' + str(ans)) might be this too (the space betweent print and parenthesis)
MicroBot
  • MicroBot
between*
anonymous
  • anonymous
Does't seem to be a problem on other print statements, seem to get error whichever way I try it, even copy paste original code !
anonymous
  • anonymous
ie. the file I download produces error
MicroBot
  • MicroBot
hmm let me see again
MicroBot
  • MicroBot
i just saw ...are u doing it in IDLE or on a new file? i mean for me even without touching the print statement it runs properly.
MicroBot
  • MicroBot
>>> while (itersLeft != 0): ... ans = ans + x ... itersLeft = itersLeft - 1 ... print (str(x) + '*' + str(x) + '=' + str(ans)) thats too much for IDLE i think...i believe u are supposed to write that long code into the other python window and then run the whole from there. i wrote the code in the IDLE and it gives me the same error it gives u.BUT, if i write it into the code window(or help me here how it is called) it works fine.
anonymous
  • anonymous
No I don't use idle just standard python shell
MicroBot
  • MicroBot
ok w8 i confused the python windows names. i wanted to say that ,when i used the shell it gave me error....but if u use [File>>>New Window] and write it there it runs normally.
anonymous
  • anonymous
I've got 2.6 and 3.3 and both show error
MicroBot
  • MicroBot
did u do it like i told u? on shell window go file>>>New Window then write ur code there and press f5 (to run it).
anonymous
  • anonymous
Ill try
anonymous
  • anonymous
same in idle, error in syntax even with the original file
anonymous
  • anonymous
Getting a little baffling
MicroBot
  • MicroBot
hmm dont know how to help u :( works fine for me...could u pass some screens maybe somehow?
anonymous
  • anonymous
Yes I will attach a screenshot if possible
anonymous
  • anonymous
Using what I see here, if I duplicate it I can get the same error in the python shell unless I hit again before the print so the ... changes to >>> Can't duplicate error in idle. x = 3 >>> ans = 0 >>> itersLeft = x >>> while (itersLeft != 0): ... ans = ans + x ... itersLeft = itersLeft - 1 ... print (str(x) + '*' + str(x) + '=' + str(ans)) File "", line 4 print (str(x) + '*' + str(x) + '=' + str(ans))
anonymous
  • anonymous
So I have attached iterate.jpg, a screenshot of my python shell running the code to an error
1 Attachment
MicroBot
  • MicroBot
minima btw do u use the suggested version of python? i mean might that be the problem? http://www.enthought.com/products/epd_free.php
anonymous
  • anonymous
I have both 2.6 and 3.3
anonymous
  • anonymous
I prefer just the shell, it should work
MicroBot
  • MicroBot
suggested is 2.7 and in the screen u r using shell (i think) not the window i told u to try.
anonymous
  • anonymous
As above I can duplicate what your image shows but hitting again like I said makes it work
anonymous
  • anonymous
Yes just the shell but it SHOULD work
anonymous
  • anonymous
OK Ill hit twice
MicroBot
  • MicroBot
xD tell us what happens when u hot twice:P
anonymous
  • anonymous
No I get the error even then
anonymous
  • anonymous
It must be something simple
MicroBot
  • MicroBot
when i paste thsi into shell: x = 3 ans = 0 itersLeft = x while (itersLeft != 0): ans = ans + x itersLeft = itersLeft - 1 print(str(x) + '*' + str(x) + '=' + str(ans)) i get no error...but i dont get any answer either:P thats crazy xD
MicroBot
  • MicroBot
if i try to write command per command it gives me the error
MicroBot
  • MicroBot
good idea:)
MicroBot
  • MicroBot
@msmithhnova ok i now got what u meant by enter twice yes it works for me aswell now.
MicroBot
  • MicroBot
no i believe u just didnt do what msmith says. before u write the print statment enter wtice so u ge the >>> then write the print statement.
anonymous
  • anonymous
Yeah the way I look at that is since you didn't hit enter again to exit the while loop code it thinks it is still in the while loop but then it gets to the print which is not indented and realizes it isn't. So now it goes "ok i am in the loop but i'm not in the loop, huh" , throws it's virtual hands up in the air and says "I give up" (syntax error)
MicroBot
  • MicroBot
hahhahah poor code!!!
anonymous
  • anonymous
... yes, you need to enter the print statement at a shell prompt you haven't let the interpreter execute the while statement/suite and it doesn't like that at all. the shell lets you enter multiline compound statements but if you try to include something 'outside' of the compund statement it will complain http://dpaste.com/810233/
anonymous
  • anonymous
did you see my code paste?
anonymous
  • anonymous
the problem is definitely an indentation problem. you have entered a multiline compound statement into the shell the print statement has been entered before the shell can process the compound statement BUT the print statement indentation places it outside of the compound statements execution suite the shell doesn't like this so it complains. try typing all that into a text file and running the file/module/script from the shell - do linux files need an extension (.py)?
anonymous
  • anonymous
Sorry folks, it is not suspect i just got it working by running it as a file in normal shell (the equivalent of running in idle) as opposed to typing into a python shell here is my result [anton@Centos ~]$ python iterstest.py 3*3 = 9 So sorry for the accusations, recriminations and the loss of around 3 hours !
anonymous
  • anonymous
bwCA is right
anonymous
  • anonymous
I can put 3.3 back on now
anonymous
  • anonymous
yeay - i don't bother much with the command prompt shell so i had to try it for myself
1 Attachment
anonymous
  • anonymous
I'm not keen on the idle so I will run them as file in shell
anonymous
  • anonymous
So we have to be careful of typing into shell with multiline statements
anonymous
  • anonymous
yep the shell only executes one 'statement' at a time. it recognizes compound statements and allows you to enter the multiple lines it (apparently) recognizes that you are done with the compound statement when you enter a blank line - then executes it if you mess up and enter two separate statements it identifies it as a syntax error don't stop using the shell tho, it is a great way to try things out and see if they work. now you know about compound statements in the shell so keep using it. when you are writing a program in a file and having trouble with it, the shell is a great place to try bits and pieces of it to narrow down the problem - often you can write simplified versions of your code in the shell to test out structure or logic and it is very convenient
anonymous
  • anonymous
And my equivalent of idle is to put it in a .py file and run it with the python command

Looking for something else?

Not the answer you are looking for? Search for more explanations.