Skip to content
ExamSpine

Lesson 2.1 · Write it

Old enough

A whole programPython or Java5 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads an age (a whole number, 0 or more) and prints a check result.

  1. Always print Age: followed by the age on the first line.
  2. If the age is 18 or more, print Allowed on the next line.
  3. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert the typed line. In Java, use Integer.parseInt.

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 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • over 18: the age and Allowed (rules 1 and 2)

    You type

    1. typed: 20

    It prints

    Age: 20
    Allowed
  • under 18: only the age (rule 1)

    You type

    1. typed: 15

    It prints

    Age: 15
solution.pySolution.java