Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to print out the values of my code using __str__ but it's not printing out
I am confused because I feel like I added all the attributes correctly and it gets all the values but just doesn't print out what I need it to print out. What I am trying to do is find the area and circumference of a circle using classes, __init__ and __str__ with the radius as 3 and pi as 3.14. I really need help and if you know any way to do this it would really help

Here is the code: 


```
class Given(object):
    def __init__(self):
        self.radius = 3
        self.pi = 3.14
        stringforradius = str(self.radius)
        stringforpi = str(self.pi)
        
        
  
def __str__(self):
    print("radius = {}, pi = {}, The area of the circle is {} ".format(self.stringforradius, self.stringforpi, self.getcircumference))
    print ("radius = {}, pi = {}, The circumference of the circle is {} ".format(self.stringforradius, self.stringforpi, self.getarea))
    

class area(Given):
    def squared(self):
        self.square = self.radius * self.radius
        
    def area(self):
        self.ar = self.pi * self.square
        self.getarea = str(self.ar)
    


class circumference(Given):
    def cir(self):
        self.cir1 = 2 * self.pi * self.radius
        self.getcircumference = str(self.cir1)
        
gettingtotal = area()
gettingtotal.squared()
gettingtotal.area()
gettingtotall = circumference()
gettingtotall.cir()
gettingtotal.__str__()
gettingtotall.__str__()
```


What I have tried:

I am confused because I feel like I added all the attributes correctly and it gets all the values but just doesn't print out what I need it to print out. 
Posted
Updated 11-Feb-22 22:55pm

The code you show doesn't print anything because you aren't defining __str__ correctly - it's not supposed to print anything itself as it shouldn't "know" what output is required.

__str__ is intended to convert a class instance to a human readable form that can be printed, saved in a file, or added to another string for later display. If it used print every time, then that wouldn't work too well!

So start here: What is the __str__ method in Python?[^] and then think about what your task requires!
 
Share this answer
 
Your class definitions are incorrect: you do not need an area or circumference class, they should b e methods of your main class. Also you never create any objects of those classes so nothing gets done. I suggest you go to the 9. Classes — Python 3.9.10 documentation[^] section of the Python tutorials and look at the explanations and samples.

Also, you do not need to convert numbers to strings and keep them internally, that will be done automatically when you print them, like this:
Python
pi = 3.14
radius = 5
circumference = radius * 2 * pi
print("radius = {}, circumference = {}".format(radius, circumference))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900