Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Ideas for a better implementation

lykelynothing (300 points)
1 2 4
in Programming in Python by (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
435 views

4 Answers

1899325 (5870 points)
1 2 17
by (5.9k 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()
b=int(b)
c=int(c)

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
by (300 points)
Thanks, it surely does get the number of lines in the code down
Nilats (3750 points)
8 14 29
by (3.8k 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 (6120 points)
14 20 65
by (6.1k points)
Hello,

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 (9820 points)
4 6 44
by (9.8k 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