# write a function that manages a simple elevator. # The function takes one argument, the floor number. # Assume there are floor numbers 1, 2, 3, 4 # When the function is invoked, # it checks if the door is currently open. # If the door is open, it prints "close" # (i.e., close the door before moving the elevator) # The function then checks the floor the elevator is currently at. # It then prints "up" (or "down") # if the elevator needs to go up (or down) # to the given floor number, followed by "open" (to open the door). # Hint: don't forget to keep track of the current floor number # and current status of the door (global vs local !!) # Example: # If function is invoked with the floor number 3 # The elevator is currently at floor number 1 (door closes) # the output will be "up up" # # If function is invoked with the floor number 3 # The elevator is currently at floor number 1 (door opens) # the output will be "close up up" # let's initialize the variables current_floor = 1 is_open = False # door is close def move_elevator(goto_floor): global current_floor global is_open result = "" if current_floor > goto_floor: move = current_floor - goto_floor if is_open: result = "close " is_open = False result += "down " * move result += "open " is_open = True else: move = goto_floor - current_floor if is_open: result = "close " is_open = False result += "up " * move result += "open " is_open = True print(result) current_floor = goto_floor move_elevator(1) move_elevator(3) move_elevator(1) move_elevator(4) move_elevator(2) # practice writing function # understand global and local variables # practice conditional statements # work with strings