Running the Area Calculator code in Terminal


#1

Hello,

I've completed the Area Calculator exercise, saved the code in .py format and tried to run it in Terminal. It seems that Terminal has problems importing items from database. Is the database used in the course something I should download separately? Could someone help? (please see the code and errors below)
Secondly, I'm a Mac user and since it has Python built in it I was under the impression I can just 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 and errors 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)*base*height
  print "Uni Bri Tri..."
  sleep(1)
  print ("Area: %.2f. \n%s" % (area, hint))
    
else:
  print "Error! Invalid shape selector specified. Exiting."

#2

.... and here are the errors I'm getting :slight_smile:

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


#3

Hi, @smallespresso86 ,

What version of Python are you using in the terminal? To find out, type ...

python -V

If it is version 3.0 or higher, then print is a function, and you must include parentheses. For example, this ...

print "The area calculator is starting up..."
print "%s/%s/%s %s:%s" % (now.month, now.day, now.year, now.hour, now.minute)

... must be changed to this ...

print("The area calculator is starting up...")
print("%s/%s/%s %s:%s" % (now.month, now.day, now.year, now.hour, now.minute))

Also, in version 3.0 and higher, raw_input no longer exists. Use input instead.


#4

Hello @appylpye ,

I originally tried to run the script on Python 2.7.10, which is run by default in Terminal, with no luck.
Someone on a separate forum suggested I get a newer version of Python and "shebang" the interpreter directive on the first line of my script before I attempt to run it, like this:

#!/usr/bin/pythonw

It fixed the problem. I noticed that installing later versions of Python does not update it automatically in Terminal, and from what I understand, I shouldn't be changing this.

I managed to run it on Python 3 using your suggestions above and the following command:

$ python3 filename.py

Now, I have 2 versions of Python 3 installed: 3.5 and 3.6. How do I know which one is my script running on? Can I specify which version of the program I want the script to run on?

Thanks!


#5

Hi @smallespresso86 ,

You can do this ...

$ python3.5 filename.py

... or this ...

$ python3.6 filename.py

... in the terminal to control which version of Python is used, since you have those versions installed.

To start an interactive session, do this ...

$ python3.5

... or this ...

$ python3.6

.


#6