HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculating the area of various 2D shapes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecalculatingshapesvariousarea

Problem

I've made a simple program that can calculate the area of various 2D shapes. I'd like your expereanced eyes to asses my code. Be as harsh as you want but please be specific and offer me ways in which I can improve my coding.

```
shape = ("triangle","square","rectangle","circle") # This is a tuple containing the string names of all the supported shapes so far.
constants = (3.14) # An object containing the value of pi. I will add more constants in future and turn this into a tuple.
start_up = input("Do you want to calculate a shape's area?: ") # Fairly self explainatory

if start_up != "yes" or "no": # This is to prevent the program failing if the user answers the start_up question
print("Sorry but I don't understand what your saying") # with an answer outside of the simple yes/no that it requires
start_up = input("Do you want to calculate a shape's area?: ") # re-iterates the start up question.

while start_up == "yes": # This is the condition that obviously contains the bulk of the program.
target_shape = input("Choose your shape: ") # Once again, self explainatory.

if target_shape == shape[0]: # Uses the shape tuple to bring forth the case for the first shape, the triangle.
h = float(input("give the height: ")) # These Two lines allow the user to input the required values to calculate the area of the triangle.
b = float(input("give the base length: ")) # They are using the float funtion to increase the accuracy of the answer.
area_triangle = h 1/2 b # The formula we all learned in school.
print("the area of the %s is %.2f" % (shape[0],area_triangle)) # This generates a format string which uses both the

Solution

Since you're not using shape extensively I would get rid of that data structure all together. It would be easier to read inline then having to look to the margin for comments.

For example:

if target_shape == 'square':                                     
     l = float(input("give the length: "))                          
     area_square = l ** 2
     print("the area of the square is %.2f" % (area_square))


When comparing strings if you don't want to be strict you should normalize them before compare.

For example:

'Square'.lower() == 'square'


so your code would become:

if target_shape.lower() == 'square':                                     
     l = float(input("give the length: "))                          
     area_square = l ** 2
     print("the area of the square is %.2f" % (area_square))


You're not accounting for any errors. What if the user hits enter without giving a length?

ValueError: could not convert string to float

Code Snippets

if target_shape == 'square':                                     
     l = float(input("give the length: "))                          
     area_square = l ** 2
     print("the area of the square is %.2f" % (area_square))
'Square'.lower() == 'square'
if target_shape.lower() == 'square':                                     
     l = float(input("give the length: "))                          
     area_square = l ** 2
     print("the area of the square is %.2f" % (area_square))
ValueError: could not convert string to float

Context

StackExchange Code Review Q#15572, answer score: 4

Revisions (0)

No revisions yet.