Defining Methods in a Class problem

Classes are a problem for me, I still can’t quite wrap my head around how to use them. I think I have this code set up right to return area and circumference but I don’t understand the change_radius method. Why do I get “change_radius takes 1 positional argument but 2 were given” here? I’ve played around with it a little bit, but get this every time.

Here is the code: https://next.tech/projects/feb95894857f/share

Thanks everyone. Hopefully this will click for me soon. :crossed_fingers:

Hey @CameronKruger!

It looks like you need to pass your radius variable to the change_radius function when you call it on line 22. I changed this and fixed a bug where pi was not defined in the code below:

import math

# Write your Circle class here
class Circle():
    def __init__(self, radius):
        self.radius = radius
        self.pi = 3.1415
        
    def area(self):
        return (self.pi * self.radius * self.radius)
    def circumference(self):
        return (2 * self.pi * self.radius)
    def change_radius(self, radius):
        self.radius = radius
        return
        
# Use this to test your code
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'

Hi Saul, thanks for replying again, you’ve been a big help.
I can see where I went wrong now, I did forget to pass that radius variable and I messed up importing the pi function from math (still learning how to do that). I edited my code to reflect that but I still cannot seem to trigger the second flag for output.
Getting:

AssertionError: 12.566 != 12.566370614359172

Hi @CameronKruger,

Looks like you need to round that result!

1 Like

I tried editing the code like this:

print("Area:", round(circle.area(), 20))
print("Circumference:", round(circle.circumference(), 20))

Which should round the result to 20 digits, right?

But when I input 2, I still only get 12.566. Could you specify how it is used to get that result of “12.566370614359172?”
Thanks again Saul.

EDIT: Okay, I just imported the pi function from the math module and used it in place of a user-defined pi. This is solved. Thanks Saul.

1 Like