Running the Area Calculator code in Terminal


#1

Hello,

I've completed the Area Calculator exercise, saved the code in .py
format and attempted to run it in Terminal. To my surprise, it seems that Terminal does not
recognize the multi line comments and has problems importing items from database. Could someone help me solve it?
Secondly, since OSX has Python built in it I was under the impression I can simply run the material from this course in Terminal. Yet, Terminal accepts 2 spaces for indentation instead of four. Is the content of this Course designed primarily for Windows users? If not, could you point me to a source of information specifying
the differences I should keep in mind when moving between the course and Terminal?

Please see the code below:

` from math import pi
from time import sleep
from datetime import datetime
now = datetime.now()
print "The area calculator is starting up..."
print "%s/%s/%s %s:%s" % (now.month, now.day, now.year, now.hour, now.minute)
sleep(1)

hint = "Don't forget to include the corect units... "

option = raw_input("Enter C for Circle or T for Triangle: ")
option = option.upper()
if option == 'C':
radius = float(raw_input("Enter the radius: "))
area = pi*(radius**2)
print "The pie is baking..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))

elif option == 'T':
base = float(raw_input("Enter the base: "))
height = float(raw_input("Enter the height: "))
area = (0.5)baseheight
print "Uni Bri Tri..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))

else:
print "Error! Invalid shape selector specified. Exiting." `

Here is the output from Terminal:


from: can't read /var/mail/math
from: can't read /var/mail/time
from: can't read /var/mail/datetime
./AreaCalculator.py: line 6: syntax error near unexpected token
('
./AreaCalculator.py: line 6: now = datetime.now()'

Thanks,
small


#2

If Python is to be said to be primarily associated with a particular OS then that would be with gnu/linux. The default terminal shell on both linux and mac is bash, but bash is not python and therefore does not read python code. Yes, mac includes python but because it is its own program, you will need to run it and tell it what code to run.

$ python mycode.py

On linux, and I presume also mac, you can mark the code file as executable and put a shebang comment at the first line to specify what interpreter to use:

$ chmod u+x mycode.py

#!/usr/bin/env python2
print 'hello world'

./mycode.py

Which is particularly useful for putting the script somewhere that is included in your PATH so that it can be executed like any other command in bash.

Any number of spaces can be used for indentation, recommended by PEP8 is 4

You may find yourself wanting to install another python version alongside the current one if the one your system is using doesn't suit you such as being a different major version (2 or 3) or being an outdated python3 (latest is 3.5, soon to be 3.6)


#3

Thanks for the intro to running python scripts on Mac. I previously tried to open the file using the "chmod a+x filename.py" and "./filename.py" commands, but for some reason it didn't work on the 2.7.10 version of Python I was running.

Opting for the latest version of Python 3 and routing the script through it with the interpreter directive you suggested fixed the problem!
Cheers:)


#4

Sounds to me like you didn't include the interpreter directive, it isn't python's fault that it isn't being executed