Practice

Validate Input in Range

2023/May/Jun·Variant 3·Q5(b)·[3 marks]
EASYLoops

Write an algorithm to ensure the variable Length is between 15 and 35 inclusive. Keep asking for input until a valid value is entered. Output Valid followed by the accepted value.

Source: Inspired by 2023 May/Jun Variant 3 Q5(b)

Input: One or more integers until a value in the range 15–35 is entered. Output: Valid then the valid value, each on a separate line.

Note: The prompt "Enter a length between 15 and 35: " is output before each input attempt.

Example:

Input:  20
Output: Enter a length between 15 and 35:
        Valid
        20
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Valid on first attempt
Inputs: 20
Expected: Enter a length between 15 and 35: Valid 20
Test 2: Two invalid then valid
Inputs: 5, 40, 15
Expected: Enter a length between 15 and 35: Enter a length between 15 and 35: Enter a length between 15 and 35: Valid 15
Test 3: Upper boundary
Inputs: 35
Expected: Enter a length between 15 and 35: Valid 35
Press Ctrl+Enter to run your code
or Ctrl+Shift+Enter to grade