Home > Software design >  Asking the user about the radius of the circle and will solve the area and the circumference
Asking the user about the radius of the circle and will solve the area and the circumference

Time:03-04

I am making a class similar code to this but I don't know how I will make it into a input function.

How would I ask the user to input a radius of a circle?

class Circle:
    def __init__(self, r):
        self.radius = r
    def area(self):
        return 3.14 * (self.radius ** 2)
    def perimeter(self):
        return 2*3.14*self.radius
obj = Circle(3)
print("Area of circle:",obj.area())
print("Perimeter of circle:",obj.perimeter())

CodePudding user response:

You just have to replace the argument to an input function to take input from the user. So the code will be changed from

obj = Circle(3)

to

obj = Circle(int(input("Please Enter Radius:")))

The int() before the input function is to convert the input from string to an integer. To know more about taking input from user, please check out here.

CodePudding user response:

class Circle:
    def __init__(self, r):
        self.radius = r
    def area(self):
        return 3.14 * (self.radius ** 2)
    def perimeter(self):
        return 2*3.14*self.radius
obj = Circle(int(input("Enter Radius:")))
#obj = Circle()
print("Area of circle:",obj.area())
print("Perimeter of circle:",obj.perimeter())
  • Related