← 2023 questions

2023 / TIKTOK / QUESTION 14

Password validation

Try the question in your own editor, then check your output below.

THE QUESTION

Sofea is creating a new account on TikTok. The app has asked her to input a password with 8 to 20 characters. Write a program that validates the input of the user. The program should begin with the prompt “input password :”. The user should be able to input the password. If the user inputs a password that doesn’t fit these requirements display the message “Please input a password containing 8 to 20 letters: ” After the message, there should be an input prompt again. This loop should occur until an appropriate password is inputted. After an appropriate password is inputted the console should display “password has been validated”. The user must then retype the password as a double-entry check. This is done by prompting the user to type “Enter the same password :” until the user types in the same password. At the end of the program the console should display “logged in”

(Explore if and while loops)

Question by Ella. Source: Girls In Code SEA — Behind the Screens, 2023, slide 30.

PRACTICE CHECK

Try your answer.

New interaction sequence. Length validation and the double-entry check are separate loops.

Run your program in your own editor, then paste its output below. This checks the stated output or checkpoint; it does not assess your full program. Practice attempts do not affect competition scores.

Practice inputs: abc, codegirls2023, wrong, codegirls2023. Enter only the three status messages in order; omit input prompts.

Reveal worked answer

Validation messages

Please input a password containing 8 to 20 letters:
password has been validated
logged in

abc is too short. codegirls2023 is valid. The first confirmation is wrong, so keep asking until the second confirmation matches.