'Asking the user about the radius of the circle and will solve the area and the circumference
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())
Solution 1:[1]
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.
Solution 2:[2]
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())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adnan taufique |
| Solution 2 | Beginner to everything |
