Area Calculator Problems


#1

'''
This is a calculator that will be able to determine the area of circles and triangles.
The user will select a shape and the calculator will calculate the area of the shape selected.
'''

from math import pi
from time import sleep
from datetime import datetime

now = datetime.now()

print "The calculator is starting up..."

print '%/%/% %:%' % (now.month, now.day, now.year, now.hour, now.minute)
sleep(1)

hint = "Don't forget to include the correct units! \nExiting... "
  
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 Bi Tri..."
sleep(1)
print ("Area: %.2f. \n%s" % (area, hint))

else:
    print "You entered garbage! The Calculator is exiting!!! "

I keep receiving an invalid syntax error on my "elif" statement. I tried indenting it, I also tried putting the actual expression (option == 'T':slight_smile: below elif and indented that as well, but I'm still getting that invalid syntax error...
I could really use another pair of eyes and/or minds because I've been staring at this for too long!!! Lol!
Any help is appreciated...

edit: the smiley face is supposed to be a colon and a closing parentheses


#2

Corrected all errors.

Here's the working code. @bo0z3ch3cha

from math import pi
from time import sleep
from datetime import datetime

now = datetime.now()

print "The 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 correct units! \nExiting... "
  
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))
    if option == 'T':
        base = float(raw_input("Enter the base: "))
        height = float(raw_input("Enter the height: "))
        area = (0.5)*base*height
        print "Uni Bi Tri..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))

#3

Main problems were your indentation, date format, and if and elif statements should work perfect now! :smiley:


#4

help! - I am not sure why this isn't working

"""
This is my first attempt at a Python area coding calculator. This program calculates the area of either a circle or a triangle!
"""

from math import pi

from time import sleep

from datetime import datetime

now = datetime.now()

print "Starting the calculator..."

print "Current time: %s/%s/%s %s:%s" % (now.month, now.day, now.year, now.hour, now.minute)

sleep(1)

hint = "Don't forget to include the correct units! \nExiting..."

option = raw_input("Enter C for Circle or T for Triangle: ")

option = option.upper()

if option == 'C':

radius = float(raw_input("Enter 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 base: "))

height = float(raw_input("Enter height: "))

area = (0.5)*base*height 

print "Uni Bi Tri..." 

sleep(1) 

print ("Area: %.2f. \n%s" % (area, hint))

else:

print "Error! Invalid shape selector specified. Exiting."

#5

Thank you so much, @bandit! That's what I get for coding hours on end with no breaks! I ran the program, but when I use t or T for triangle, it doesn't give me the option to enter a value. It just goes straight to exiting... This exercise is driving me CRAZY! :sweat_smile:

Thanks again!


#6

from math import pi
from time import sleep
from datetime import datetime

now = datetime.now()

print "The 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 correct units! \nExiting... "
  
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 Bi Tri..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))
else:
		print "You entered garbage! The Calculator is exiting!!! "

I figured it out!!! Thanks again, @bandit! I would've went crazy without your help and steering me in the right direction! It is now fully functional!


#7

What's wrong with mine?


#8

Let me take a look at yours :smiley:


#9
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 Bi Tri..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))
else:
		print "You entered garbage! The Calculator is exiting!!! "

Indent your if, elif, and else statements like so and then..

print '%s/%s/%s %s:%s' % (now.month, now.day, now.year, now.hour, now.minute)

Change your print statement like that, should work after that! :slight_smile:


#10

Thanks! I knew I was close, I just couldn't figure out what was wrong


#11

Haha sometimes it's the small details that really get us in trouble :sweat_smile:


#12

Sorry, @blakew483... I would've helped you out, but I haven't been on since my last post! Glad you got it figured out, though. @bandit to the rescue! :smile:


#13

Haha :sweat_smile: Happy to have helped!


#14