【发布时间】:2025-06-13 00:35:02
【问题描述】:
import math
import random
# === Problem 1
class RectangularRoom(object):
"""
A RectangularRoom represents a rectangular region containing clean or dirty
tiles.
A room has a width and a height and contains (width * height) tiles. Each tile
has some fixed amount of dirt. The tile is considered clean only when the amount
of dirt on this tile is 0.
"""
def __init__(self, width, height, dirt_amount):
"""
Initializes a rectangular room with the specified width, height, and
dirt_amount on each tile.
width: an integer > 0
height: an integer > 0
dirt_amount: an integer >= 0
"""
self.width, self.height, self.dirt_amount = width, height, dirt_amount
tiles = [(w,h) for w in range(width) for h in range(height)]
self.room = {tile:dirt for tile in tiles for dirt in [dirt_amount]}
#raise NotImplementedError
def get_width(self):
return self._width
def set_width(self, value):
if value <= 0 :
raise ValueError("Must be greater than 0")
self._width = value
width = property(get_width,set_width)
def __str__(self):
return str((self.room))
到目前为止,这就是我对这个房间对象所做的事情。我正在尝试使高度,dirt_amount 也限制为 int 并且大于零或大于等于零。是否有更简单或更有效的方法来为其他两个属性编写这些约束?
【问题讨论】:
-
等一下@crystalizeee,这是不是格式错误,或者你没有缩进类下面的函数?
-
天哪,当我从 IDE 复制粘贴时,它的格式错误。你能忽略这个愚蠢的错误吗?谢谢!
标签: python-3.x decorator