创建一个可以打印角坐标的python Rectangle对象类 [英] Creating a python Rectangle object class that can print the corner coordinates

查看:68
本文介绍了创建一个可以打印角坐标的python Rectangle对象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手.我需要创建一个 python Rectangle 对象类,当调用它时,可以打印角的坐标以及面积和周长.当我尝试运行我的代码时遇到问题:

I am new to python. I need to create a python Rectangle object class that when called upon one can print the coordinates of the corners as well as have the area and perimeter. I am having issues when I try to run my code that says:

<__main__.Rectangle instance at 0x02F20030>

我被告知也要添加 __str__ 但后来我得到:

I was told to add the __str__ as well but then I get:

TypeError: __str__ returned non-string (type int)

任何帮助将不胜感激,谢谢!

Any help would be appreciated, thanks!

class Rectangle:
    def __init__(self, topLeft, topRight, bottomLeft, bottomRight):
        self.tL = topLeft
        self.tR = topRight
        self.bL = bottomLeft
        self.bR = bottomRight
    def perim(self):
            return (2 * (self.tL + self.tR)) + (2 * (self.bL + self.bR))
    def area(self):
            return (self.tL + self.tR) * (self.bL + self.bR)
    def position(self):
        return self.tL
        return self.tR
        return self.bL
        return self.bR
def __repr__(self):
        return self.tL
        return self.tR
        return self.bL
        return self.bT


r1 = Rectangle (5, 5, 10, 10)
print r1

推荐答案

试试这个:

    def __repr__(self):
        return 'tL = '+str(self.tL) + ', tR ='+ str(self.tR)+', bL ='+ str(self.bL) + ', bR =' +str(self.bR)

注意事项:

  • 在一个函数中只执行一个 return 语句,在你的代码中你的函数只返回 self.bT(最后一个).
  • 在提供的代码中 def __repr__(self) 没有缩进.
  • 定义矩形不需要 4 个点.
  • 您的矩形角不应该只是整数,它们必须是两个坐标 (x, y) 的序列,例如 (3, 7) 是一个点,将一个点实现为 2 个整数元组或列表.
  • make only one return statement execute in a function, in your code your function only returns self.bT (the last).
  • In the provided code def __repr__(self) is not indented.
  • you don't need 4 points to define a rectangle.
  • your Rectangle corners shouldn't just be integers, they have to be sequences of two coordinates (x, y) for instance (3, 7) is a point, implement a point as a 2 integers tuple or list.

正如 OP 所问,这里是如何更改您的 __init__ 方法以使用笛卡尔坐标:

edit: as the OP asked, here's how to change your __init__ method to work with cartesian coordinates:

class Rectangle:
    def __init__(self, tL, bR):  #tL and bR should be passed as tuples
        self.tL = tL
        self.tR = (bR[0], tL[1])  #access tuple elements with tuple[index]
        self.bL = (bR[1], tL[0])
        self.bR = bR

        self.width = bR[0]- tL[0]
        self.height = bR[1] - tL[1]
    def area(self):
        #get area
        #...
    def perim(self):
        #get perim
        #...

r1 = Rectangle((5,5), (30, 20))
print r1.tL   #output (5, 5)
print r1.tR   #output (30, 5)
print r1.width #output 25
print r1.area() #output 375

当然你可以创建一个Point class 来代替那个,然后你将两个Point 传递给__init__ 来定义矩形.

of course you can create a Point class instead of that, and then you pass two Points to __init__ to define the rectangle.

希望对你有帮助!

这篇关于创建一个可以打印角坐标的python Rectangle对象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆