Area calculator

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

#area calculator
def areacalculator():
    _input_ = input("Enter the shape you want to calculate area of: ")
    area = 0
    pie = 3.14
    if _input_ == "Square":
        side = int(input("Enter the value of side: "))
        area = area + (side ** 2)
    elif _input_ == "Circle":
        radius = int(input("Enter the value of radius: "))
        area = area + (2 * pie * radius)
    elif _input_ == "Rectangle":
        length = int(input("Enter the value of length: "))
        width = int(input("Enter the value of length: "))
        area = area + (length * width)
    elif _input_ == "Triangle":
        base = int(input("Enter the value of base: "))
        height = int(input("Enter the value of height: "))
        area = area +(0.5 * base * height)
        print ("Select a valid shape")
    print ("%.2f" % area)


Did you read the ask a good question topic? Please include some content, what are you struggling with? What error message do you get?

Hi. What am I doing wrong in my area calculator code. The code for circle seams to work, but not the rest. Thanks.

This program calculates area of shapes.

print "The area calculator has started"

# shape options
option = raw_input("Enter 'C' for circle or 'T' for triangle: ")

# calculating area of circle
if option == 'C':
		radius = float(raw_input("what is the radius of the circle in meters (m)?: "))
		area_c = 3.14159 * radius ** 2
print "The area of the circle is " + 	str(area_c) + " m2)"

# calculating area of triangle
elif option == 'T':
  base = float(raw_input("what is the base measurement (m)?: "))
  height = float(raw_input("what is the height measurement (m)?: "))
  area_t = 0.5 * base * height
print "the area of the triangle is " + str(area_t) + " m2"

# dealing with any other entry
else: print "This entry or shape is not currently supported by this app, please restart the app and choose either 'C' or 'T' when prompted to do so." % (option)

print "END of program"


elif should always be after an if or another elif:

if condition:
   print "do something"
elif another condition:
  print "do something else"

hi everyone. I’m working on areacalculator. I know I didn’t follow all the directions, but I do want to know why the console asks the user for radius, height, and base every time regardless of whether the user enters C and T. Can anyone divine from the code as to what I could do better?

#these are the definitions of the variables
option = raw_input("select a shape. Print C for circle and T for triangle: ")
size_C = float(raw_input("enter the radius: "))
size_T1 = float(raw_input("enter the height: "))
size_T2 = float(raw_input("enter the base: "))

circle_area = size_C ** 2 * 3.14159
triangle_area = (size_T1 * size_T2) / 2

#this is the actual program in python
def areacalculator():
 print"this program allows you to select a shape, then calculates its area, and then prints the area of that shape to you." 
 print "the calculator is now starting up"
 print option
 if option == "C" or option == "c":
  print size_C
  print "your circle area is " + str(circle_area)
 elif option == "T" or option == "t":
  print size_T1
  print size_T2
  print "your triangle size is " + str(triangle_area)
  print "you need to enter a shape"
 print"the program is exiting"


Clearly, you didn’t follow the directions. My suggestion would be to go back, and follow the directions. Your code is kind of a mess. The reason your code asks the user for either ‘C’ or ‘T’ and then radius, height, and base every time is because those are the first 4 lines of code. They get executed no matter what the user inputs for any of them. I’m not trying to pick on you, but seriously, you need to follow the directions. It would almost be easier to re-write your code from scratch than to fix it. Try again, following the directions, and re-post your code if you are still having problems. Good luck && happy coding!

1 Like

Hi there, Thanks for your reply. I actually had already successfully completed the exercise, but I had just wanted to practice coding a different way after completion as practice and to understand Python at a deeper level. Therefore, I wanted to make sense of this alternative code and why it doesn’t work (not as per assignment directions, but conceptually). I’m curious why if I’m calling just the appropriate measurements for circle and triangle at each point (i.e. asking for radius for circle and base and height for triangle), why all three still get asked for in the console. I hope that makes sense.
Thanks again!

– edit: I found the reason why: “print” is not necessary for those raw_input commands to be displayed on the console. Sorry for the bother, and thanks!

1 Like

Ah. That makes more sense now :slightly_smiling_face: That’s cool that you are experimenting with code. My apologies for coming across a little harsh in my first reply. I didn’t realize that you were trying something out after having completed the exercise.
You are correct that your raw_input statements are being executed as soon as your program runs. You could put them in their own function that you call from within your areacalculator() function, or just move them to the appropriate place within the function. You do need either an if ... else or a switch to control the flow, so you don’t ask for all of the inputs regardless of the user’s selection of ‘C’ or ‘T’. Good luck. Keep up the good work && happy coding!

i dont think decimals work with this

Write a Python program to find area of i) Square, ii) Rectangle, iii) Circle. Take input of all the values need to calculate these areas from the user with appropriate prompts. Display all the values with appropriate titles.
while True :
print("\n***** MENU *****\n")
print("Enter 1. For Square ")
print(“Enter 2. For Rectangle”)
print(“Enter 3. For Circle”)
print("Enter 0. For Exit or Quite ")
c = int(input(“Enter Your Choice : “))
if c == 1 :
print(”\n SQUARE \n”)
l = float(input("Enter Length of Square : "))
Sarea = l * l
print(“Area of Square : “,Sarea)
elif c == 2 :
print(”\n RECTANGLE \n”)
l=float(input("Enter Length of Rectangle : "))
b=float(input("Enter Breadth of Rectangle : "))
if l == b :
print (“this is square::”)
Rarea= l * b
print("Area of Square : ",Rarea)
else :
Rarea= l * b
print(“Area of Rectangle : “,Rarea)
elif c == 3 :
r=float(input("Enter Radious of Circle : "))
Carea= 3.14 * r * r
print("Area of Circle : ",Carea)
elif c == 0 :
print(“you are out from quit…”)
else :
print("Please Enter Valid Choice… ")

and what is your question? I only see a requirements and code dump