'How to limit MAX and MIN when working with the top and bottom floors?

The code below defines an Elevator class. The elevator has a current floor, it also has a top and a bottom floor.

Those are the minimum and maximum floors it can go to.

Fill in the blanks to make the elevator go through the floors requested.

class Elevator:
    def __init__(self, bottom, top, current):
        """Initializes the Elevator instance."""
        self.bottom = 0
        self.top = 0
        self.current = 0
    def up(self):
        """Makes the elevator go up one floor."""
        self.current += 1
    def down(self):
        """Makes the elevator go down one floor."""
        self.current -= 1
    def go_to(self, floor):
        """Makes the elevator go to the specific floor."""
        self.current = floor
    def __str__(self):
         return '"Current floor: {}"'.format(self.current)

elevator = Elevator(-1, 10, 0)

elevator.up()
elevator.current #should output 1
print(elevator.current)

elevator.down()
elevator.current #should output 0
print(elevator.current)

elevator.go_to(10)
elevator.current #should output 10
print(elevator.current)

# Go to the top floor. Try to go up, it should stay. Then go down.
elevator.go_to(10)
elevator.up()
elevator.down()
print(elevator.current) # should be 9
# Go to the bottom floor. Try to go down, it should stay. Then go up.
elevator.go_to(-1)
elevator.down()
elevator.down()
elevator.up()
elevator.up()
print(elevator.current) # should be 1

elevator.go_to(5)
print(elevator)


Solution 1:[1]

Please see code below:

class Elevator:
def __init__(self, bottom, top, current):
    """Initializes the Elevator instance."""
    self.bottom = bottom
    self.top = top
    self.current = current
def up(self):
    """Makes the elevator go up one floor."""
    self.current = self.current + 1 if self.current < self.top else self.current
def down(self):
    """Makes the elevator go down one floor."""
    self.current = self.current - 1 if self.current > self.bottom else self.current
def go_to(self, floor):
    """Makes the elevator go to the specific floor."""
    self.current = floor
def __str__(self):
     return '"Current floor: {}"'.format(self.current)

Hope this is helpful,

Regards, Gino

Solution 2:[2]

class Elevator:
def __init__(self, bottom, top, current):
    """Initializes the Elevator instance."""
    self.bottom = bottom
    self.top = top
    self.current = current
def up(self):
    """Makes the elevator go up one floor."""
    if self.current < 10:
        self.current += 1  
def down(self):
    """Makes the elevator go down one floor."""
    if self.current > 0: 
        self.current -= 1
def go_to(self, floor):
    """Makes the elevator go to the specific floor."""
    self.current = floor
def __str__(self):
    return "Current floor: {}".format(self.current)

elevator = Elevator(-1, 10, 0)

Solution 3:[3]

class Elevator: def init(self, bottom, top, current): """Initializes the Elevator instance.""" self.bottom = bottom self.top = top self.current = current def up(self): """Makes the elevator go up one floor.""" self.current = self.current + 1 if self.current < self.top else self.current def down(self): """Makes the elevator go down one floor.""" self.current = self.current - 1 if self.current > self.bottom else self.current def go_to(self, floor): """Makes the elevator go to the specific floor.""" assert floor <= self.top and floor >= self.bottom self.current = floor def str(self): return '"Current floor: {}"'.format(self.current)

elevator = Elevator(-1, 10, 0)

Solution 4:[4]

after Checking JupyterLab and trying to solve this exercise I found that this answer was giving the correct answers. Try it:

class Elevator:
    def __init__(self, bottom, top, current):
        self.bottom = bottom
        self.top = top
        self.current = current
        """Initializes the Elevator instance."""
        pass

    def up(self):

        if self.current >= self.top:
            self.current = self.top
        else:
            self.current += 1
        """Makes the elevator go up one floor."""
        pass
    def down(self):
        if self.current <= self.bottom:
            self.bottom = self.bottom
        else:
            self.current -=1
        """Makes the elevator go down one floor."""
        pass
    def go_to(self, floor):
        self.current = floor
        """Makes the elevator go to the specific floor."""
        pass
    def __str__(self):
        return "Current floor {}".format(self.current)

elevator = Elevator(-1, 10, 0)

elevator.up()
elevator.current #should output 1

elevator.down()
elevator.current #should output 0

elevator.go_to(10)
elevator.current #should output 10

# Go to the top floor. Try to go up, it should stay. Then go down.
elevator.go_to(10)
elevator.up()
elevator.down()
print(elevator.current) # should be 9
# Go to the bottom floor. Try to go down, it should stay. Then go up.
elevator.go_to(-1)
elevator.down()
elevator.down()
elevator.up()
elevator.up()
print(elevator.current) # should be 1

elevator.go_to(5)
print(elevator)

Solution 5:[5]

class Elevator:
    def __init__(self, bottom, top, current):
        self.bottom=bottom
        self.top=top
        self.current=current
    def up(self):
          if self.current!=self.top:
            self.current=self.current+1
    def down(self):
          if self.current!=self.bottom:
            self.current=self.current-1
    def go_to(self, floor):
        """Makes the elevator go to the specific floor."""
        self.floor=floor
        self.current=self.floor
    def __str__(self):
         return "Current floor: {}".format(self.current)

Solution 6:[6]

class Elevator:
    def __init__(self, bottom, top, current):
        """Initializes the Elevator instance."""
        self.bottom = bottom
        self.top = top
        self.current = current
    
        pass
    
    def up(self):
        """Makes the elevator go up one floor."""
        if self.current < self.top:
            self.current += 1
        else: 
            self.current
        
        pass
    def down(self):
        """Makes the elevator go down one floor."""
        if self.current > self.bottom:
            self.current -=1
        else:
            self.current
        pass
    def go_to(self, floor):
        """Makes the elevator go to the specific floor."""
        self.current = floor
        pass
    def __str__(self):
        return "Current floor: {}".format(self.current)
        pass

elevator = Elevator(-1, 10, 0)

Solution 7:[7]

class Elevator:
    def __init__(self, bottom, top, current):
        """Initializes the Elevator instance."""
        self.bottom = bottom
        self.top = top
        self.current = current
        
    def __str__(self):
        return "Current floor: {}".format(self.current)
        
    def up(self):
        """Makes the elevator go up one floor."""
        if self.current < self.top:
            self.current += 1
        
    def down(self):
        """Makes the elevator go down one floor."""
        if self.current > self.bottom:
            self.current -= 1
            
    def go_to(self, floor):
        """Makes the elevator go to the specific floor."""
        if floor >= self.bottom and floor <= self.top :
            self.current = floor

Solution 8:[8]

class Elevator:
    def __init__(self, bottom, top, current):
    """Initializes the Elevator instance."""
        self.current = 0
        self.bottom = -1
        self.top = 1
    def up(self):
    """Makes the elevator go up one floor."""
        self.current += 1
    def down(self):
    """Makes the elevator go down one floor."""
        self.current -= 1
    def go_to(self, floor):
    """Makes the elevator go to the specific floor."""
        self.current = floor
    def __str__(self):
        return "current floor:{}".format(self.current)

elevator = Elevator(-1, 10, 0)



# Go to the top floor. Try to go up, it should stay. Then go down.
elevator.go_to(10-1)
elevator.up()
elevator.down()
print(elevator.current) # should be 9
# Go to the bottom floor. Try to go down, it should stay. Then go up.
elevator.go_to(-1+2)
elevator.down()
elevator.down()
elevator.up()
elevator.up()
print(elevator.current) # should be 1

9
1

elevator.go_to(5)
print(elevator)

current floor:5

Solution 9:[9]

class Elevator: def init(self, bottom, top, current):

    """Initializes the Elevator instance."""
    self.bottom = bottom
    self.top = top
    self.current = current
def up(self):
    """Makes the elevator go up one floor."""
    if self.current < self.top:
        self.current += 1  
def down(self):
    """Makes the elevator go down one floor."""
    if self.current > self.bottom: 
        self.current -= 1
def go_to(self, floor):
    """Makes the elevator go to the specific floor."""
    self.current = floor
def __str__(self):
    return "Current floor: {}".format(self.current)

elevator = Elevator(-1, 10, 0)

Solution 10:[10]

class Elevator:
    def __init__(self, bottom, top, current):
        self.bottom = bottom
        self.top = top
        self.current = current
        """Initializes the Elevator instance."""
        pass
    def up(self):
        """Makes the elevator go up one floor."""
        if self.current < self.top:
            self.current += 1
        pass
    def down(self):
        """Makes the elevator go down one floor."""
        if self.current > self.bottom:
            self.current -= 1
        pass
    def go_to(self, floor):
        """Makes the elevator go to the specific floor."""
        if self.current < self.top and self.current > self.bottom:
            self.current = floor
        pass

elevator = Elevator(-1, 10, 0)