The problem is that you are doing (1/3)
which will evaluate to 0
. This is because python's integer division (and in fact most programming languages' integer division) will truncate or round-down. Do this instead:
def main():
coneHeight = 4.5
coneR = 1.5
pi = 3.141
cone = pi * (1.0/3) * coneHeight * (coneR * coneR)
print cone
if __name__ =='__main__':main()
Or you could do:
from __future__ import division
def main():
coneHeight = 4.5
coneR = 1.5
pi = 3.141
cone = pi * (1/3) * coneHeight * (coneR * coneR) #no need to do 1.0/3 anymore
print cone
if __name__ =='__main__':main()
As for your second question,
the __name__
variable is assigned the value '__main__'
when the script is run directly (from the shell, cmd, bash, etc). Therefore, your script will only run when this .py file is run directly and main()
will not be called if it is imported by another file.