Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

python 3.x - How do I get rid of the quotes and parentheses in my output?

Here is the code: I am supposed to make the code print out the area of the shapes. That part does work. But in the output there are the parentheses, quotation marks, and commas. How do I get rid of these pieces to make the output more clean?

import math
#adding parent class
class Shape:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y
#adding child classes and methods
class Circle(Shape):
    def __init__(self, X, Y):
        super().__init__(X, Y)
        self.X = X
        self.Y = Y

    def Area(self):
        return 'Circle with radius', self.X, 'has an area of', math.pi * self.X**2

class Rectangle(Shape):
    def __init__(self, X ,Y):
        super().__init__(X, Y)
        self.X = X
        self.Y = Y

    def Area(self):
        return 'Rectangle with a width of', self.X, 'and a height of', self.Y, 'has an area of', self.X * self.Y

class Triangle(Shape):
    def __init__(self, X, Y):
        super().__init__(X, Y)
        self.X = X
        self.Y = Y

    def Area(self):
        return 'Triangle with a base of', self.X, 'and a vertical height of', self.Y, 'has an area of', 0.5 * self.X * self.Y

class Trapezoid(Shape):
    def __init__(self, X, Y, Z):
        super().__init__(X, Y)
        self.X = X
        self.Y = Y
        self.Z = Z

    def Area(self):
        return 'Trapezoid with a long width of', self.X, 'and a short width of', self.Z, 'and a vertical height of', self.Y, 'has an area of', (0.5 * (self.Z + self.X)) * self.Y

if __name__ == '__main__':
#Main information
    c = Circle(5, 0)
    r = Rectangle(3, 4)
    t = Triangle(6, 2.7)
    tr = Trapezoid(7, 8, 4)
#Printing the output
    print(c.Area())
    print(r.Area())
    print(t.Area())
    print(tr.Area())
question from:https://stackoverflow.com/questions/65923659/how-do-i-get-rid-of-the-quotes-and-parentheses-in-my-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are returning a tuple of strings. Instead, use f' string notation. For example:

    def Area(self):
        return f'Trapezoid with a long width of {self.X} and a short width of {self.Z} and a vertical height of {self.Y} has an area of {(0.5 * (self.Z + self.X)) * self.Y}'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...