Ideas for a better implementation

lykelynothing (300 points)
1 2 4
asked Oct 16, 2021 in Programming in Python by lykelynothing (300 points)
I was looking at the last exerciese that prof. Mancini published on replit and i was wondering if anyone could think of a more efficent and simpler way to implement its solution.

My code is the following:

import math

def AreaCalculator():

type_of_figure = input("First of all write what kind of shape: ")

if type_of_figure == "circle":

radius = int(input("What is the radius of this circle? "))

return math.pi*radius**2

elif type_of_figure == "square":

side = int(input("What is the side of the square? "))

return side**2

elif type_of_figure == "rectangle":

greater_side = int(input("What is the greater side of the rectangle? "))

smaller_side = int(input("What is the smaller side of the rectangle? "))

return greater_side*smaller_side

elif type_of_figure == "triangle":

base_lenght = int(input("What is the lenght of the base? "))

height = int(input("What is the height of the triangle? "))

return base_lenght*height/2

I'm sure there are better solutions to this than just if statements, feel free to suggest

4 Answers

1899325 (5690 points)
0 0 15
answered Oct 16, 2021 by 1899325 (5,690 points)
A useful thing to do is to define a function for multiple input such as this:

a,b,c=input("Give the shape and the 2 measures needed for the Area, if only one is needed insert 0 for the third input ").split()

So that you call this function and you just need the if and elif statements for the shapes in which you can avoid the input of the measures and only return the area
lykelynothing (300 points)
1 2 4
commented Oct 17, 2021 by lykelynothing (300 points)
Thanks, it surely does get the number of lines in the code down
Nilats (3730 points)
6 10 28
answered Oct 18, 2021 by Nilats (3,730 points)
I don't think this code per se need a lot of optimization, maybe a useful tip is to create a function for every shape and then just call it in the main function(AreaCalculator()).
Luigi Pizza (6080 points)
11 19 65
answered Oct 18, 2021 by Luigi Pizza (6,080 points)

I don't want to be that type of guy, but we shouldn't exchange code. I've done this exercise a little bit more complicated that it needed to be: I also made possible for the user to use Heron's formula (the one that calculates the area of a triangle based on the measure of the 3 sides). But yes, the split method is kind of necessary if you want to reduce the number of inputs you want to have.

More informations about .split():
its sintax is: my_string.split(separator, limit)
And It returns a list of separated strings.


if you omit the separator, its base value is " ". In general, separator is the recurring value thanks to which you want to separate the elements of the list.
limit is the maximum amount of elements in the list that we want to split:

For Example for my_string = "bas/44/33/print(ln(x))/19/10/2021", mylist.split("/", 4) will return:
['bas', '44', '33', 'print(ln(x))', '19/10/2021']
because it doesn't touch whatever else is after the value of limit
gianluca5539 (9740 points)
2 4 42
answered Oct 19, 2021 by gianluca5539 (9,740 points)

You can also use numbers to make the selection in place of the shape's name, like in most command line tools. It becomes easier for the user because it eliminates the possibility of wrong word casing or spelling. 

It should then look something like this:

Available shapes:

1. Square

2. Circle

3. Triangle 

Please select your shape (1-3): 

And so on. 

This allows the user to know exactly what the application can do and leaves no room for errors to occur as I said before.

Hope you find my answer helpful, goodbye