Why here need a () when I wrote print(medium_pizza.circumference())?
And since def init return none, why when I test print(medium_pizza.radius), it did return radius value? and why in there I don’t need a () after radius?
class Circle:
pi = 3.14
def __init__(self, diameter):
print("Creating circle with diameter {d}".format(d=diameter))
# Add assignment for self.radius here:
self.radius = diameter / 2
def circumference(self):
self.circumference= 2 * self.pi * self.radius
return self.circumference
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
print(medium_pizza.circumference())
print(teaching_table.circumference())
print(round_room.circumference())
print(medium_pizza.radius())
init is a magic method, its automatically called when you create a new instance of a class. Within the init method, you can then declare the instance variable(s)
radius is instance variable, accessing variable, instance variable and class variables doesn’t require parentheses.
why in the test we wort the answer for Q 3 before Q 2 and when i answer 2 first it give me error that i need to define medium_pizza and i already did so i don’t understand this part
Question 2 asks you to Define three Circle s with three different diameters.
A medium pizza, medium_pizza , that is 12 inches across.
Your teaching table, teaching_table , which is 36 inches across.
The Round Room auditorium, round_room , which is 11,460 inches across.
3.
Define a new method circumference for your circle object that takes only one argument, self , and returns the circumference of a circle with the given radius by this formula:
circumference = 2 * pi * radius
What you see in that link is what you get when you click “View Solution.” Question 2 asks you to declare the diameters of the different objects, whereas Question 3 asks you to set up the .circumference method. However, when you do it in that order, you get an error, and even the answer key shows that you should do the step outlined in Question 3 before Question 2. The text should probably be flipped for those two questions.
I’m really not sure and I wish I had copied it when it happened. This has happened a couple times in the Classes lessons where I put in something that returned an error only to find nothing different in the answer key.
The same thing happened to me. My code was correct when I compared it to the solution; however, I also had to put the block of code defining the diameters of the three objects from step 2 after the block defining the circumference method from step 3. I could not get it to run without error without switching these. Once you move the method above the objects, it runs fine, but they are not written in the editor in the same order the instructions are given.
Can someone please advise if there is a certain order code must be written within the class? i.g. the init statement, then methods, then objects, then print statements. Or can a method be defined after objects are declared? Does it matter?
That can be quite revealing. It suggests that we may extend a class with a global function. The only problem I can foresee with this is that if get_name() gets changed, it will directly affect the method we added to the class.
We can even delete the global function without affecting any existing instances.
>>> del(get_name)
>>> id(get_name)
Traceback (most recent call last):
File "<pyshell#89>", line 1, in <module>
id(get_name)
NameError: name 'get_name' is not defined
>>> foo.get_name()
'bar'
>>>
That includes for new instances; witness,
>>> bar = Foo('foo')
>>> bar.get_name()
'foo'
>>>
As to the order we write the methods in the class it will take some more experimentation and documentation research. This is something that could well be explored at a later time so as not to disrupt your progress.
Same thing happened to me.
Struggled to sovle it but gave up.
I just placed it with a soulution.
Still do not know why same code that I wrote does not apply.
class Circle:
pi = 3.14
def __init__(self, diameter):
print("Creating circle with diameter {d}".format(d=diameter))
# Add assignment for self.radius here:
self.radius = diameter / 2
def circumference(self):
return 2 * self.pi * self.radius
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
The error message says - “Traceback (most recent call last):
File “script.py”, line 1, in
class Circle:
File “script.py”, line 9, in Circle
medium_pizza = Circle(12)
NameError: name ‘Circle’ is not defined”
Thank you in advance for the answer that you will provide.
When sharing code here, please make sure you format it accordingly, this is especially important for languages like Python that are so sensitive to needing the correct indentation/formatting etc as the forums software will not keep any of this if you just copy-paste it in normally:
Having tested it, the issue seems to be that the three statements at the bottom where you instantiate the class are indented by various degrees, so the Python interpreter sees them as being inside of the class, and therefore it doesn’t work, try removing the indentation for those three lines, so they lie outside of the class.