──────────────────────────────────────

42. Build Your Own Python Program · Collect Inputs Until the User Is Done

Read a list of values that can vary in length and stop cleanly.

Lesson files · lessons/build-your-own-program/keep-and-count-input

Before you start
- Repeat steps with a while loop and exit with break.
- Store values in a list with append().
- Clean text with strip() and lower().

By the end
- Loop over input until a user types the agreed stop word.
- Skip empty entries and keep the useful ones for the final result.

Where you can use it
Let someone enter club names, item labels or measurements without knowing the number of lines in advance.

Use a stop word when the number of entries can change. Read a line, take off surrounding spaces, and check the stop word before saving it. lower() lets the user type different capitals for the stop word. Keep the original cleaned name if you want to show it later.

An empty line has its own meaning. Decide to ignore it and go around the loop again. Append each useful item once, and print the count after the loop has stopped. Test a run where the first entry is the stop word.

Predict
Why does the example finish when the input is CLOSE?
- The input function changes capitals into lowercase.
- It compares the lowercase form with close.

Worked example (example.py)

while True:
    answer = input().strip()
    if answer.lower() == "close":
        break
    print(f"Note: {answer}")
print("Notebook closed")


Example input
 Put out chairs 
 CLOSE 

Example result
Note: Put out chairs
Notebook closed

Read club names until the user enters done. Ignore empty or space-only lines, and recognise done in any mix of capitals. Print Clubs counted: followed by the number entered. Count repeated names as separate entries.

Starter program (starter.py)

names = []
while True:
    name = input().strip()
    if name.lower() == "done":
        break
    names.append(name)
print(f"Clubs counted: {len(names)}")


Starter input
Chess
 
Robotics
 DONE 

Hint
Keep the list outside the loop. Check for the stop word first and only append a name when the cleaned line has text.

Case 1. Try the sample
Input
Chess
 
Robotics
 DONE 
Expected output
Clubs counted: 2

Case 2. Stop immediately
Input
done
Expected output
Clubs counted: 0

Case 3. Same name twice
Input
Chess
Chess
Done
Expected output
Clubs counted: 2

Stretch
Print the first name after the count when the list has at least one item.

Reflect
What happens if you check for the stop word after storing the line?

Check your prediction
It compares the lowercase form with close. lower() produces the lowercase text used in the condition.
