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('The area is', answer)
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
answer = '{:.2f}'.format((s*(s-TRIANGLE['a'])*(s-TRIANGLE['b'])*(s-TRIANGLE['c'])) ** 0.5)
print('The area is', answer)
break
else:
print('Not enough data')
break
def cal_square():
side = int(input('Input the side of the square: '))
answer = side * side
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):
answer='{:.2f}'.format(pi*(CIRCLE['r']**2))
print('The area of the circle is {}. This can be calculated by multiplying pi(3.14) by {} squared'
.format(answer,CIRCLE['r']))
CIRCLE['r'] = 0
break
elif(CIRCLE['d']!=0):
answer='{:.2f}'.format((pi/4)*(CIRCLE['d']**2))
print('The area of the circle is {}. This can be calculated by dividing pi(3.14) by 4, and multiplying it to '
'the diameter of {} squared'.format(answer,CIRCLE['d']))
CIRCLE['d'] = 0
break
elif(CIRCLE['c']!=0):
answer= '{:.2f}'.format((CIRCLE['c']**2)/(4*pi))
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
Enter your data: c 40
Enter your data: done 1
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