-1

I tried to write an OOPS python program to calculate the volume of cylinder but when I run my code I get"Process returned 0". I need to define class "Cylinder", take input from the user. Below is my code. What am I doing wrong?

from math import pi


# FUNCTIONS
def cylinder_volume(radius, height):
    return ((pi * (radius ** 2))*height)


# main
def Cylinder():
    radius = float(input("Radius="))
    height = float(input("Height="))

    print("Cylinder volume: %d" % (cylinder_volume(radius, height)))

# PROGRAM RUN
    if __name__ == "__Cylinder__":
        Cylinder()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    You did not define a class `Cylinder`. You defined a function. Classes start with `class`. – Tim Roberts Jan 17 '22 at 04:04
  • 1
    The "Process returned 0" happened because (a) you have the indentation wrong on the last two lines, so those are actually part of the function; thus, there was no code to execute, and (b) once you fixed that, it still wouldn't run because the `__name__` variable has the value `"__main__"`, not `"__Cylinder__"`. – Tim Roberts Jan 17 '22 at 04:10
  • Do not change the functionality of the code in your post, because that makes the comments and answers nonsensical. You also removed the actual calculation. I rolled it back for you. – wjandrea Jan 17 '22 at 04:31
  • Honestly, you're doing a lot of things wrong. Have you learned OOP, and do you know how to define classes? Do you know [the purpose of `if __name__ == "__main__":`](/q/419163/4518341)? – wjandrea Jan 17 '22 at 04:45

1 Answers1

2

Here's how you would do that in a object-oriented way. You define a Cylinder object, which owns the radius and the height. It's up to the caller to do the I/O; the object only holds the state and has methods. Then, the object has a volume method that returns the cylinder volume.

And the __name__ variable when you run a Python script is always "__main__".

import math

class Cylinder():
    def __init__(self, radius, height):
        self.radius = radius
        self.height = height

    def getVolume(self):
        return math.pi * (self.radius ** 2) * self.height

if __name__ == "__main__":
    radius = float(input("Radius="))
    height = float(input("Height="))
    cyl = Cylinder(radius, height)
    print("Cylinder volume:", cyl.getVolume())
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30