-1

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())

2 Answers2

3

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.

Adnan Taufique
  • 379
  • 1
  • 9
1
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())
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 28 '22 at 16:52