Lesson 2.2 · Write it
Range check
A whole programPython or Java6 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a program that reads a whole number and checks whether it is in the range 1 to 100, inclusive.
- Always show
Number:followed by the number on the first line. - If the number is at least 1 and at most 100, show
In rangeon the next line. - 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. You need two conditions: one for the lower bound and one for the upper bound.
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 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
in the middle of the range (rules 1 and 2)
You type
- typed: 50
It prints
Number: 50 In range
below the range (rule 1 only)
You type
- typed: 0
It prints
Number: 0
the lower boundary, included (rule 2)
You type
- typed: 1
It prints
Number: 1 In range