I just started to learn python few days ago. This is a small program I made to practise.

print("Find Volumes of some Popular Solids")
print("")
names=['(1) Sphere','(2) Right Circular Cone','(3) Cylinder']
x=0
while x < 3:
    print(names[x])
    x +=1



print('\n' + 'Enter the number of the solid, you want to check the volume and press ENTER')

solid=int(input('Number : ' ))

print('\n'+'You have selected : ', (names[solid]))

x = 22/7

if solid == 1 :
    R=int(input('\n'+"Enter value for r : "))
    V=float((4*(22/7)*R) / 3)
    H=0

elif solid == 2 :

    R=int(input('\n'+"Enter value for r : "))
    H=int(input("Enter value for h : "))
    V = float((x) *(R*R) * H) * (1/3)

elif solid ==3 :

    R=int(input('\n'+"Enter value for r : "))
    H=int(input("Enter value for h : "))
    V = float((22/7)*(R*R)*H)

else :

    print("Entered value is wrong")


R=str(R)
H=str(H)
V=str(V)

print('\n'+"h = " + H + "  r = " + R)
print("Volume is : "+ V + '\n'+ '\n')

input("Press ENTER key to exit the program")

Dani AI

Generated

Nice start, . A few practical fixes will make this more accurate and easier to extend. First, use math.pi instead of 22/7, and prefer float over int so users can enter decimals. Second, the sphere formula should be (4/3)*pi*r**3 (you currently miss the r**3). Third, watch the off-by-one when mapping menu choices to a list; if the user types 3, names[3] is out of range. Finally, separate calculation from input/output so you can test the math easily, as suggested.

Here is a small, structured version that validates input, handles bad choices, and prints results cleanly:

import math

def sphere(r): return (4.0/3.0) * math.pi * r**3
def cone(r, h): return math.pi * r**2 * h / 3.0
def cylinder(r, h): return math.pi * r**2 * h

def ask_pos_float(label):
    while True:
        try:
            v = float(input(label))
            if v > 0: return v
        except ValueError:
            pass
        print("Please enter a positive number.")

actions = {
    "1": ("Sphere", lambda: sphere(ask_pos_float("r: "))),
    "2": ("Right circular cone", lambda: cone(ask_pos_float("r: "), ask_pos_float("h: "))),
    "3": ("Cylinder", lambda: cylinder(ask_pos_float("r: "), ask_pos_float("h: "))),
}

while True:
    print("\nFind volumes (1-3) or q to quit:")
    for k in ("1", "2", "3"):
        print(f"{k}) {actions[k][0]}")
    choice = input("> ").strip().lower()
    if choice in ("q", ""): break
    if choice in actions:
        name, fn = actions[choice]
        print(f"{name} volume: {fn():.4f}")
    else:
        print("Invalid choice.")

Tips: keep variable names lowercase (r, h), avoid converting numbers to strings before printing, and guard the error path so you never reference variables that were not set.

I just started to learn python few days ago. This is a small program I made to practise.

Ok,some points.

names = ['(1) Sphere','(2) Right Circular Cone','(3) Cylinder']
x=0
while x < 3:
    print(names[x])
    x +=1

This is not the way to loop in Python.
You just iterate over list like this.

names = ['(1) Sphere','(2) Right Circular Cone','(3) Cylinder']
for name in names:
    print(name)

Now all code is global space,in future think of stucture code better.
Eg using functions.

import math

def cylinder():
    '''Calculate volume of a cylinder'''
    R = int(input('\n'+"Enter value for r : "))
    H = int(input("Enter value for h : "))
    result = math.pi * (R*R) * H
    return result

Test.

>>> cylinder()
Enter value for r : 5
Enter value for h : 10
785.3981633974483

>>> help(cylinder)
Help on function cylinder in module __main__:

cylinder()
    Calculate volume of a cylinder

So you could had have 3 functions,this mean that code is isolatet in function.
As you see it help readability of code,and code reuse(you can now put this function in other code and call it).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.