I'm having technical issues... even following along during the lesson, writing the code out exactly as it is on the handouts, I always come to a point where my indentations don't line up, and cause me syntax errors(usually at the print stage). what do I need to do to fix this?
What version of Python are you using? 2.x or 3.x? Any of the 2 line should work well with the handouts. The 3.x line will have problems, especially with print statements. That is why I bring it up.
I'm using 2.5.4 I downloaded from the course intro. With single lines of code I have no print issues... hang on I'll post an example, maybe you can see what I'm doing wrong.
If you put ``` above and below a code block it will retain formatting. ``` for (int i = 0; i < 5; i++) { print "bla" } ``` That is just a psudocode example. But you can see how the ``` (accent on ~ key) helps.
def cube(number): """Takes a number and returned the cube of that number. Input: number (float or int) Output: number (float)""" return number**3 At this point I hit enter to go to the next line and print, and the indentation remains in line with "return" as if im going to continue scripting. So I realign it with "def" and I get a syntax error when I type "print cube(3)" no quotations, of course.
OK. In idle you can change indentation with ... lets see... ctrl [ ctrl ] The ctrl [ will de-indent.
``` def cube(number): """Takes a number and returned the cube of that number. Input: number (float or int) Output: number (float)""" return number**3 print cube(3) ```
>>> def cube(number): """Takes a number and returns the cube of that number. Input: number(float or int) Output: number(float)""" print cube(9) SyntaxError: invalid syntax So when I tried the ctrl trick, it went as you can see past the >>>. trying to realign in front of ">>>" also always results in a syntax error. still not sure what I'm doing wrong :(
Also, if there are spaces and tabs, the indent can look the same but be different! Ah, the shell acts a bit differently than the editor. You may need to be very careful about how you enter it in the shell.
got it, I realized my dumb mistake haha, thanks!
Kk. Was it the fact that you did not hit enter one more time so that it actually defined it?
no, I've been doing all my work in the shell haha. simple lines were no problem, but more involved stuff had me pulling my hair out, but then when I would paste it back into the shell, it worked fine.
Join our real-time social learning platform and learn together with your friends!