Skip to content
ExamSpine

Lesson 3.4 · Write it

Lift distance

A whole programPython or Java10 tests, 7 of them hidden

On a phone? Put the lines in order instead

Write a program that adds up how many floors a lift travels. It asks for the floors the lift goes to, one at a time, until the user types the word stop, then prints the total.

The lift starts on floor 0. Your program asks for each floor with exactly this prompt, which ends with a colon and one space and no line break:

Next floor:

Each answer is either a whole number, which is the floor the lift moves to, or the word stop. Floors below 0 are basement floors, so -2 is two floors below the ground floor. When the answer is stop, the program prints exactly one line:

Total floors travelled: N

where N is the total number of floors the lift moved, up or down, starting from floor 0 and going to every floor in the order typed.

  1. A move counts the floors between the floor the lift is on and the next floor, whichever way the lift goes: take the lower floor from the higher one. Moving from floor 2 to floor -1 is 3 floors.
  2. An answer that is the floor the lift is already on adds 0.
  3. stop ends the input: after it, the program asks for nothing more. If the first answer is stop, the program prints Total floors travelled: 0.
  4. Everything your program prints is checked exactly: every prompt, and the total line with its capital T, its colon and its spaces. What the user types is not part of what your program prints, so each prompt is followed straight away by the next one, and the total line comes right after the last prompt. Spaces at the end of a line are ignored.
  5. Every answer is either a whole number from -1000 to 1000 or the word stop in lower case, and stop always comes.

In Python, write the program at the top level of the file and read each answer with input("Next floor: "). In Java, write the main method, public static void main(String[] args), print each prompt with System.out.print and read each answer with one Scanner. If you paste a whole class instead, its main method is run.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 10 times: the 3 examples below, and 7 hidden ones that check the rules above. Only what it prints is checked.

  • up 5 floors, down 3, then up 4 (rules 1 and 4)

    You type

    1. typed: 5
    2. typed: 2
    3. typed: 6
    4. typed: stop

    It prints

    Next floor: Next floor: Next floor: Next floor: Total floors travelled: 12
  • down past the ground floor, then the same floor again (rules 1 and 2)

    You type

    1. typed: 4
    2. typed: -1
    3. typed: -1
    4. typed: stop

    It prints

    Next floor: Next floor: Next floor: Next floor: Total floors travelled: 9
  • stop as the first answer (rule 3)

    You type

    1. typed: stop

    It prints

    Next floor: Total floors travelled: 0
solution.pySolution.java