# Shape area calculator

I am a student who is a beginner at Python, and decided to make a simple program to help me finish my homework. While the program below will probably not help me with my homework, it is still one of my first mini projects. It would be great if you review my code and tell me how I could make it more efficient, or how I could accomplish my task but with less code. I would also appreciate it if you help me point out the flaws of my code and how I could improve it.

from __future__ import(print_function, division)
import __future__
import statistics
from statistics import StatisticsError

TRIANGLE = {'w': 0, 'h': 0, 'a': 0, 'b': 0, 'c': 0}   #dict which stores the data of the triangle
CIRCLE = {'r': 0, 'd': 0, 'c': 0}

def cal_triangle():
print('If you have data of width and height, enter w [data]\n for example:\nw 50\nh 10\nThis will multiply the '
' number according to the data given above (the width and height)')
print('If you know sides a, b or c, you can enter like (similarly):\na 5\n b 4\nc 3\n')
print('When you are done writing your data, enter done 1')
while True:
command, number= input('Enter the data needed to solve the area: ').split()
number = int(number)
#Gathering data into our dict
if(command=='w'):
TRIANGLE['w'] = number
elif(command=='h'):
TRIANGLE['h'] = number

if(command=='a'):
TRIANGLE['a'] = number
elif(command=='b'):
TRIANGLE['b'] = number
elif(command=='c'):
TRIANGLE['c'] = number
if(command=='done'):
if(TRIANGLE['w']!=0 and TRIANGLE['h']!=0):   #If the data is filled
answer = (int(TRIANGLE['w']) * int(TRIANGLE['h'])) / 2
print(
'This is calculated by multiplying the height of {} to the width of {}, and dividing it by two'.format(
TRIANGLE['h'], TRIANGLE['w']))
#Clearing the data, to be filled again later
TRIANGLE['w'] = 0
TRIANGLE['h'] = 0
break
elif(TRIANGLE['a']!=0 and TRIANGLE['b']!=0 and TRIANGLE['c']!=0):   #If data is filled
#Calculating perimeter
s = (TRIANGLE['a'] + TRIANGLE['b'] + TRIANGLE['c'])/2
#Area using Heron's Formula
break
else:
print('Not enough data')
break

def cal_square():
side = int(input('Input the side of the square: '))
print('The area of the square is:', answer)
print('The perimeter of the square is:', side*4)
print('The area of {} could be found by multiplying {} by {}. This is because all squares have the same side.'.format(answer, side, side))
print('The perimeter of {} could be found by multiplying {} four times. This is because a perimeter has four sides'.format(side*4,side))

def cal_rectangle():
w = int(input('Input the width of the square: '))
h = int(input('Input the height of the square: '))
area = w*h
perimeter = (w+h)*2
print('The area of the rectangle is {}. This value can be obtained by multiply the width of {} to the height of {}'
.format(area,w,h))
print('The perimeter of the rectangle is {}. This can be obtained by adding the width and height and multiplying it by two'
.format(perimeter))

def cal_circle():
print('The data that you input must be either ')
pi = 3.14
while True:
command, number = input('Enter your data: ').split()
number = int(number)
#Gathering data
if(command=='c'):
CIRCLE['c'] = number
elif(command=='d'):
CIRCLE['d'] = number
elif(command=='r'):
CIRCLE['r'] = number

elif(command=='done'):
# The program will decide which formula to use according to the data given by the user
if(CIRCLE['r']!=0):
print('The area of the circle is {}. This can be calculated by multiplying pi(3.14) by {} squared'
CIRCLE['r'] = 0
break
elif(CIRCLE['d']!=0):
print('The area of the circle is {}. This can be calculated by dividing pi(3.14) by 4, and multiplying it to '
CIRCLE['d'] = 0
break
elif(CIRCLE['c']!=0):
print('The area of the circle is {}. This can be calculated by multiplying {} squared to four PIs'.format(answer, CIRCLE['c']))
CIRCLE['c']= 0
break
else:
print('Not enough data')
break

print('Enter a shape for which you would like to calculate (pick either a triangle, rectangle, square'
' or circle')
while True:
shape = input(
'Shape: '
)
if(shape=='triangle'):
cal_triangle()
elif(shape=='rectangle'):
cal_rectangle()
elif(shape=='square'):
cal_square()
elif(shape=='circle'):
cal_circle()
elif(shape==''):
break
else:
print('Pick a proper shape')


When this program is used, something like this will show up on the console:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/****/PycharmProjects/projects/areaCal.py
Enter a shape for which you would like to calculate (pick either a triangle, rectangle, square or circle
Shape: triangle
If you have data of width and height, enter w [data]
for example:
w 50
h 10
This will multiply the  number according to the data given above (the width and height)
If you know sides a, b or c, you can enter like (similarly):
a 5
b 4
c 3

When you are done writing your data, enter done 1
Enter the data needed to solve the area: a 50
Enter the data needed to solve the area: b 10
Enter the data needed to solve the area: c 4
Enter the data needed to solve the area: done 1
The area is 0.00+595.66j
Shape: circle
The data that you input must be either
The area of the circle is 127.39. This can be calculated by multiplying 40 squared to four PIs
Shape: square
Input the side of the square: 5
The area of the square is: 25
The perimeter of the square is: 20
The area of 25 could be found by multiplying 5 by 5. This is because all squares have the same side.
The perimeter of 20 could be found by multiplying 5 four times. This is because a perimeter has four sides
Shape: rectangle
Input the width of the square: 4
Input the height of the square: 3
The area of the rectangle is 12. This value can be obtained by multiply the width of 4 to the height of 3
The perimeter of the rectangle is 14. This can be obtained by adding the width and height and multiplying it by two
Shape:

Process finished with exit code 0

• what is an area of "0.00+595.66j"? what is j? – miracle173 Aug 1 '17 at 5:11
• @miracle173 That's a miscalculation in my part – raven rogue Aug 1 '17 at 5:16
• from the path of the areaCal.py fle you display we can deduce the username. I always prefer not to publish such personal data in my post. – miracle173 Aug 1 '17 at 6:22

When reading your output I found that the user has to terminate his input by entering

done 1


1 does not make sense. So I think you wrote an input routine that requires a string and a number. But you should change this.

done


should be sufficient. You should not adapt the user to your input routine but adapt the input routine to the needs of the user.

Some of the output is nonsense, e.g.

• when processing a rectangle you put out: 'the height of the square is ...
• the area of the square '0.00+595.66j' is nonsense
• after 'Shape: circle' the meaningless phrase 'The data that you input must be either ' is printed

I think you should review your output and first cleanup your program before you request a code review. If you are not interested in the output then print only as much as is necessary, e.g. print only the result of the calculation.

Python is not C / C++ / Java and doesn't require parentheses in the if statements.

You program works for integer dimensions only - why not use float() instead of int() to work with arbitrary numbers?

pi = 3.14


use

import math
PI = math.pi       # Note uppercase letter for a constant


    #Gathering data into our dict
if(command=='w'):
TRIANGLE['w'] = number
elif(command=='h'):
TRIANGLE['h'] = number

if(command=='a'):
TRIANGLE['a'] = number
elif(command=='b'):
TRIANGLE['b'] = number
elif(command=='c'):
TRIANGLE['c'] = number


you may use shorter

    #Gathering data into our dict
if command in 'whabc':
TRIANGLE[command] = number


        if(CIRCLE['r']!=0):


(and similar statements) you may use more Pythonic

        if CIRCLE['r']:


as nonzero values have boolean value True.

CIRCLE = {'r': 0, 'd': 0, 'c': 0}


you may simply immediately after getting input convert it to the radius and then for calculating the area use only the formula for radius (math.pi * r * r).

You have to check the input values for the triangular sizes
(e. g. by triangular inequalities a + b > c and so on)
to refuse bad input values for the sake to get rid of meaningless outputs as

Enter the data needed to solve the area: a 50
Enter the data needed to solve the area: b 10
Enter the data needed to solve the area: c 4
Enter the data needed to solve the area: done 1
The area is 0.00+595.66j

(the complex number) in your sample output.

TRIANGLE = {'w': 0, 'h': 0, 'a': 0, 'b': 0, 'c': 0}   #dict which stores the


you may use shorter

TRIANGLE = dict(zip('whabc', (0,) * 5))


The explanation:

zip() takes 2 sequences - whabc and 00000 and makes pairs of corresponding values: (w, 0), (h, 0) and so on. The dict() constructor is able to create an appropriate dictionary from the sequence of pairs.

Note:

(0) == 0 but (0,) is a tuple, so (0) * 5 == 0 but (0, ) * 5 == (0, 0, 0, 0, 0).

Long literal strings as in your statement

print('The area of the rectangle is {}. This value can be obtained by multiply the width of {} to the height of {}'
.format(area,w,h))


can be broken into parts by simple write individual parts one next other:

print('The area of the rectangle is {}. '
'This value can be obtained by multiply the width of {} to the height of {}'
.format(area,w,h))


(continuation on the second line is allowed without \ symbol at the end of the first one because they are both in the same parentheses).

And you may use names in {} placeholders for improving the readability:

print('The area of the rectangle is {area}. '
'This value can be obtained by multiply '
'the width of {width} to the height of {height}'
.format(area=area, width=w, height=h))


(and as a bonus you obtain the possibility of arbitrary ordering them in the format() parameter list).