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

Hello folks :) Concerning to the first code, 2nd lecture, where the task is to find the cube of a given number. I've tried to run the program and when the number I entered was not a perfect cube, I got as an outcome the statement 'X is not a perfect cube'and 'Cube of x is anw', ans being a guess. I changed the code and apparently it's working now (I shall put it on the comments). Did you people have any problems on trying to run it? P.S. I've just started this course.

OpenStudy (anonymous):

Here's my modified code: x=int(raw_input('Enter a number: ')) ans=0 while ans*ans*ans<abs(x): ans=ans+1 if ans*ans*ans!=x: print 'X is not a perfect cube' if ans*ans*ans==x and x>0: print 'Cube of '+str(x)+'is'+str(ans) else: if ans*ans*ans==x and ans<0: ans=-ans print 'Cube of ' + str(x) + ' is: ' + str(ans)

OpenStudy (anonymous):

If you want to show code snippets in your post, surround them with bacticks (left of 1 on qwerty keyboard). three backticks code code three backticks ``` x=int(raw_input('Enter a number: ')) ans=0 while ans*ans*ans<abs(x): ans=ans+1 if ans*ans*ans!=x: print 'X is not a perfect cube' if ans*ans*ans==x and x>0: print 'Cube of '+str(x)+'is'+str(ans) else: if ans*ans*ans==x and ans<0: ans=-ans print 'Cube of ' + str(x) + ' is: ' + str(ans) ```

OpenStudy (anonymous):

You can also use code pasting sites - http://dpaste.com/1422590/ - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste.

OpenStudy (anonymous):

any ideas on this: "invalid syntax" error while trying to run PS1: Computing primes/ and Cube root problem? Thanks!

OpenStudy (anonymous):

Hello, Jonathan. Please write down your code here so I can check it. Thanks, buddy.

OpenStudy (rsmith6559):

invalid syntax is a frequent error. A typo, mismatched brackets, parentheses, curly braces. Missing colon. To be honest, if I code more than two lines, I expect to get them.

OpenStudy (anonymous):

I see your point. Thing is, since I've set up a loop, I expect it to stop when you get what you want. In this case, if the given number is not a perfect cube, the right answer is 'It's not a perfect cube'. What the code gives me is that the given number is not a perfect cube and 'Cube of x is' ans+increment. Perharps I've done something wrong. But hmmm I just tested the code we were given. Any idea?

OpenStudy (anonymous):

Well, I've just found out what the error was about: indentation. The last if was in the wrong place. My mistake. Sorry. I shall close this question. Thanks guys.

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!