HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Problems
  2. /A long scenario question. A website asks each new user for a password…
HardComputer ScienceHigh School (9–10)Computer Science › ProgrammingcodingIGCSE

A long scenario question. A website asks each new user for a password…

StemPy LabContributed by StemPy Lab

Try it on paper first. A hint, the key idea and the worked solution are here when you need them.

A long scenario question. A website asks each new user for a password. A password is accepted when:

  • it has from 8 to 16 characters;
  • it holds at least one upper-case letter;
  • it holds at least one character that is not a letter (a digit or a symbol).

Write a program with a function that checks one password and returns TRUE or FALSE. The main program asks for a password until one is accepted, with a message for each rejected one. After 3 rejected tries it outputs Account locked. Otherwise it outputs how many tries were needed. Use constants and comments. Then dry-run it for the typed passwords sunshine and Sun5hine.

Check your answer

Work it out on paper, then type your number. The answer itself stays hidden.

Need a reminder? The key ideaShowHide

A maintainable program uses meaningful identifiers, comments, constants for values that may change, and procedures and functions for separate jobs. In the 15-mark scenario question, list every task, plan the data, write the program in commented parts, and dry-run it.

The worked solution

Have you tried it?

Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.

A model answer in pseudocode.

pseudocode
CONSTANT MinLength ← 8
CONSTANT MaxLength ← 16
CONSTANT MaxTries ← 3

// TRUE when the password follows all three rules
FUNCTION CheckPassword(Password : STRING) RETURNS BOOLEAN
    DECLARE Index : INTEGER
    DECLARE Letter : CHAR
    DECLARE HasUpper : BOOLEAN
    DECLARE HasOther : BOOLEAN
    IF LENGTH(Password) < MinLength OR LENGTH(Password) > MaxLength
      THEN
        RETURN FALSE
    ENDIF
    HasUpper ← FALSE
    HasOther ← FALSE
    FOR Index ← 1 TO LENGTH(Password)
        Letter ← SUBSTRING(Password, Index, 1)
        // An upper-case letter changes when LCASE is used
        IF UCASE(Letter) = Letter AND LCASE(Letter) <> Letter
          THEN
            HasUpper ← TRUE
        ENDIF
        // A digit or a symbol is the same in both cases
        IF UCASE(Letter) = LCASE(Letter)
          THEN
            HasOther ← TRUE
        ENDIF
    NEXT Index
    RETURN HasUpper AND HasOther
ENDFUNCTION

DECLARE Attempt : STRING
DECLARE Tries : INTEGER
DECLARE Accepted : BOOLEAN
Tries ← 0
Accepted ← FALSE
REPEAT
    OUTPUT "Enter a new password"
    INPUT Attempt
    Tries ← Tries + 1
    Accepted ← CheckPassword(Attempt)
    IF NOT Accepted
      THEN
        OUTPUT "Password rejected"
    ENDIF
UNTIL Accepted OR Tries = MaxTries
IF Accepted
  THEN
    OUTPUT "Password accepted after ", Tries, " tries"
  ELSE
    OUTPUT "Account locked"
ENDIF

The dry run. sunshine has 8 characters, but no upper-case letter and nothing that is not a letter, so it is rejected. Sun5hine has 8 characters, the capital S and the digit 5, so it is accepted. The output ends with Password accepted after 2 tries.

How such an answer is marked. One mark for each point, up to 15:

  1. Constants for the lengths and the number of tries.
  2. A function with a STRING parameter that returns a BOOLEAN.
  3. The length check, 8 to 16, both included.
  4. A loop over every character of the password.
  5. Taking one character with SUBSTRING.
  6. A correct test for an upper-case letter.
  7. A correct test for a character that is not a letter.
  8. The function returns TRUE only when all three rules hold.
  9. A prompt and an input in the main program.
  10. A loop that asks again after a rejected password.
  11. A message for each rejected password.
  12. A count of the tries.
  13. The loop stops after 3 rejected tries.
  14. The final messages: locked, or accepted with the number of tries.
  15. Meaningful identifiers and comments.

Watch out. Testing only the first character, or returning TRUE as soon as one rule holds, are the most common slips.

Log in to save this for later
← All Problems
← This function should return the larger of two integers, and the main program should output twice…A weather app stores one temperature in degrees Celsius for each day of a week… →
More onComputer Science › Programming

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Maintainable programs and the long scenario questionProcedures, Functions and Strings in Pseudocode

More on this topic

  1. A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a…Hard
  2. Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…Medium
  3. The file Best.txt holds one line: 120. What is output, and what does Best.txt hold afterwards, when…Easy
The nine squares of a board are input row by row: X, O, O, then X, X, X, then O, a dot for an empty…Hard
  • Three teams play three games each. Points[Team, Game] holds the points of a team in a game…Medium
  • All problems on this topic →