DEV Community

Mohammad Alsuwaidi
Mohammad Alsuwaidi

Posted on

Volume calculator

i made a shape volume calculator tell me if i can improve it even more

# this is volume calculator according to calculator.net
import sys
from math import sqrt

shape = float(input(
    "Enter 1 for sphere, 2 for cone, 3 for cube, 4 for cylinder, 5 for Rectangular Tank, 6 for Capsule, \
7 for Conical Frustum, 8 for Ellipsoid, 9 "
    "for Square Pyramid, 10 for Tube, 11 for Spherical Cap: "))

if shape == 1:
    r = int(input("enter the radius of the sphere: "))
    an = 1.33333333333 * 3.141592653589793 * r ** 3
    unit = raw_input("Enter the unit: ")
    print an, unit, "^3"
elif shape == 2:
    r = int(input("Enter the base radius : "))
    h = int(input("Enter the height of the cone: "))
    an = 0.33333333333 * 3.141592653589793 * r ** 2 * h
    unit = raw_input("Enter the unit: ")
    print an, unit, "^3"
elif shape == 3:
    l = int(input("Enter the length of one edge: "))
    an = l ** 3
    unit = raw_input("Enter the unit: ")
    print an, unit, "^3"
elif shape == 4:
    r = float(input("Enter the base radius: "))
    h = float(input("Enter the height of the cylinder: "))
    an = 3.141592653589793 * r ** 2 * h
    unit = raw_input("Enter the unit: ")
    print an, unit, "^3"
elif shape == 5:
    l = int(input("Enter the length of Rectangular Tank: "))
    w = int(input("Enter the width of Rectangular Tank: "))
    h = int(input("Enter the height of Rectangular Tank: "))
    an = l * w * h
    unit = raw_input("Enter the unit: ")
    print an, unit, "^3"
elif shape == 6:
    r = int(input("Enter the base radius: "))
    h = int(input("Enter the height: "))
    unit = raw_input("Enter the unit: ")
    an = 1.33333333333 * 3.141592653589793 * r ** 3 + 3.141592653589793 * r ** 2 * h
    print an, unit, "^3"
elif shape == 7:
    r = int(input("Enter Top Radius: "))
    b = int(input("Enter Bottom Radius: "))
    h = int(input("Enter height: "))
    unit = raw_input("Enter the unit: ")
    an = 0.33333333333 * 3.141592653589793 * h * (r ** 2 + r * b + b ** 2)
    print an, unit, "^3"
elif shape == 8:
    a1 = int(input("Enter the first axis: "))
    a2 = int(input("Enter the second axis: "))
    a3 = int(input("Enter the third axis: "))
    unit = raw_input("Enter the unit: ")
    an = 1.33333333333 * 3.141592653589793 * a1 * a2 * a3
    print an, unit, "^3"
elif shape == 9:
    b = int(input("Enter the Base Edge: "))
    h = int(input("Enter the Height: "))
    unit = raw_input("Enter the unit: ")
    an = 0.33333333333 * 3.141592653589793 * b ** 2 * h
    print an, unit, "^3"
elif shape == 10:
    d1 = int(input("Enter the outer diameter: "))
    d2 = int(input("Enter the inner diameter: "))
    l = int(input("Enter the length of tube: "))
    unit = raw_input("Enter the unit: ")
    an = 3.141592653589793 * (d1 ** 2 - d2 ** 2) / 4 * l
    print an, unit, "^3"
elif shape == 11:
    RR = int(input("Enter 1 if u have base radius or 2 if u have ball radius: "))
    if RR == 1:
        r = int(input("Enter the Base radius: "))
        h = int(input("Enter the Height: "))
        unit = raw_input("Enter the unit: ")
        R = (h ** 2.0 + r ** 2.0) / (2.0 * h)
        an = 0.33333333333 * 3.141592653589793 * h ** 2 * (3 * R - h)
        print an, unit, "^3"
    elif RR == 2:
        R = int(input("Enter the ball radius: "))
        h = int(input("Enter the Height: "))
        r = sqrt(2 * R * h - h ** 2)
        unit = raw_input("Enter the unit: ")
        an = 0.33333333333 * 3.141592653589793 * h ** 2 * (3 * R - h)
        print an, unit, "^3"
else:
    sys.exit()


Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
madalinfasie profile image
madalinfasie • Edited

You can get rid of the if-elif statement, by creating a function for each shape volume computation and then create a dictionary linking the shape number (or name honestly) to the callback such as: mapping = {1: volumne_sphere, 2: volume_cone...}. Then you can do mapping[shape]() only once and the requested volume computation will be automatically selected. If you change the input to be a string (user writes 'sphere' instead of 1, the dictionary will be more readable too).

If you can reuse some functions, that's even better.

Notice the lack of parantheses from the function names inside the dictionary, you only want the reference to that function, and the calling will be done on extraction from dict.

Also, you can use math.pi to get the pi constant and 1/3 instead of 0.333333 and 1.3333 can be written as 1 + 1/3. Makes the code cleaner.

If you're using python3, you can replace raw_input with input.

Collapse
 
tupac profile image
Mohammad Alsuwaidi

i am really new idk anything about functions or anything can you please help me with it i am sorry for bothering you

Collapse
 
madalinfasie profile image
madalinfasie

A function is only a named block of code. You can "call" that block of code using the function name, so that code becomes reusable. You should definitely spend some time reading about it. I recommend sentdex on YouTube, or real python in written form. Good luck

Thread Thread
 
tupac profile image
Mohammad Alsuwaidi

thank you so much i would def look into it