# Write your Circle class here. This is my code. class Circle: def __init__ (self, circle): self.circle = circle def radius (self, radius): self.radius = radius def area(self, radius): area = 3.14 * radius ** 2 def circumference (self, radius): circumference = 2 * 3.14 * radius
I tried to figure out what is my error. The output of the problem is:
Traceback (most recent call last): File "main.py", line 20, in <module> circle.change_radius(radius) AttributeError: 'Circle' object has no attribute 'change_radius'
The auto-filled code problem is:
if __name__ == '__main__': circle = Circle(7) keepgoing = True while keepgoing: radius = float(input("Circle radius: ")) circle.change_radius(radius) print("Area:", circle.area()) print("Circumference:", circle.circumference()) yn = input('Keep going? Y/N ') keepgoing = yn == 'y' or yn == 'Y'
And the problem say:
Write a Python class named
Circle, constructed by a radius and two methods, which will calculate the circumference and the area of a circle. The script should ask for the user’s input for the radius, create a
Circle object, and print out its area and circumference. It should ask for input again after it prints the area and circumference each time. Our aim here is to practice defining methods in a class.
The steps are as follows:
- Create a file named main.py.
- Define the Circle class and the Circle constructor method.
- Create the
areacalculation method, which returns the circle’s area.
- Create the
circumferencemethod, which returns the circle’s circumference.
- After the class definition, add the code that requests for user input for the radius.
- We’ll create a
whileloop so that the request for user input runs multiple times. In the
whileloop, we’ll request the user input, change the
circleobject’s radius, and then print out the area and circumference.
- You can run the script by using
python main.py. The script will ask for input, calculate the area and circumference, print that out, and ask for your input again. The output should look as follows: