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

Hi. Looking for advice on setting up Python 2.7 on my Mac. I downloaded the latest 3.5 before I learnt that 2.7 is desired. When looking for 2.7 download I found out that 2.7 is built into my Mac terminal. I have had success entering commands into my Mac terminal up until the point of the 2.2 "Branching Programs" (text book) with if, else statements, which I could not get to work. Anyone else who set up Python 2.7 on their Mac? How did you go about it?

OpenStudy (rsmith6559):

Python v2 is installed on OSX by default. IDLE is also installed by default. In the last couple of versions of OSX, IDLE is available via Terminal with the command: idle & The ampersand allows IDLE to run in the background so that the Terminal window is still usable. Alternately, you could use Text Edit, vi, emacs or pico to create a PLAIN TEXT FILE of your program which you can execute in Terminal with: python /path/to/your/program Be careful with TextEdit. It's default file format is rtf (Rich Text Format), which won't work for programs. You have to specifically save your program as plain text to be usuable. Alternately, you could make the first line of your program: #!/usr/bin/python and change the permissions of the file to executable with: chmod 0755 /path/to/your/program Then you can just call your program in Terminal and it will execute. If you want to learn more about the chmod command: man chmod

OpenStudy (dovetales108):

Thank you very much for that. Din't know I could use idle from Terminal also. I still keep getting this problem where my if/else statements get syntax errors. Here is an example: once I click enter after the 4th line the error is shown and I cannot type in for example "print 'odd'" >>> x = 7 >>> if x%2 == 0: print 'even' else: SyntaxError: invalid syntax Can you please explain what I am doing wrong?

OpenStudy (rsmith6559):

Python reads blocks of code by indentation. C derived languages have blocks in curly braces. Your code copied from my terminal: >>> x = 7 >>> >>> if x%2 == 0: ... print "even" ... else: ... print "odd" ... odd >>> The two print statements are two blocks of code, so they're indented from the if/else statement. Just an FYI, IDLE converts tabs into spaces, which can make your indentation look correct, but actually it isn't because one has tabs and the other has spaces. This is why I gave up on IDLE.

OpenStudy (anonymous):

x = 7 if x%2 == 0: print 'even' else: print 'odd'

OpenStudy (rsmith6559):

Did it run okay?

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!