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 и либо больше нуля или больше и равен нулю.Есть ли более простой или более эффективный способ кодирования этих ограничений для двух других атрибутов?