-1

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)
1
  • 2
    Well, reason it through for yourself. Can you think of a way to check, for example, whether the result of self.current += 1 is in range? If it is not in range, what should you do about it? Apr 17, 2020 at 3:30

11 Answers 11

2

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)
2
  • Hello, Gino. Thanks a lot again. You directed me in the right vector. Can I ask you check code below regarding: assert floor <= self.top and floor >= self.bottom. And how to add a readable text if got mistake, but not rough Python text error? Apr 18, 2020 at 0:41
  • the init function should be init. regarding error handling, you can refer to this link: docs.python.org/3/tutorial/errors.html Apr 19, 2020 at 11:02
0

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)
0
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)
1
  • Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – rizerphe
    Jun 29, 2020 at 12:35
0
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)
0
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)

2
  • Please consider providing an explanation of the solution you are providing. Oct 25, 2020 at 15:09
  • if self.current < 10: - what if the top floor is bigger?
    – Tomerikoo
    Dec 29, 2022 at 10:41
0
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
0
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)
1
  • Please refrain from posting code alone without any explanation. May 12, 2022 at 14:16
0
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)
0
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)
0

This gave me correct answer.

 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 f"Current floor: {self.current}"
    
    def up(self):
        """Makes the elevator go up one floor."""
        if self.current < self.top:
            self.current = self.current +1
        return self.current
    def down(self):
        """Makes the elevator go down one floor."""
        if self.current  > self.bottom:
            self.current = self.current -1
        return self.current
    def go_to(self, floor):
        """Makes the elevator go to the specific floor."""
        self.current = floor
        return self.current

elevator = Elevator(-1, 10, 0)
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 28, 2022 at 7:12
-1
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
1
  • You don't use any of the arguments of __init__ and you changed the testbench so it would work...
    – Tomerikoo
    Dec 29, 2022 at 11:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.