0%

Repeating Work with Loops · practice

Keep Asking Until the Answer Makes Sense

Sometimes a should not end; it should simply skip one unusable and try the next one. That is the job of continue.

When Python reaches continue, it abandons the rest of the current pass and returns to the top of the loop.

Skip one value, not the whole loop

Start with a for loop, where “the next pass” is easy to see:

Try it

The empty is skipped. The loop does not end; it moves on to "no". That is the difference to keep straight:

  • break ends the loop.

  • continue skips the remaining body. A for loop then asks for the next item; a while loop checks its again.

For a short body, a plain can be clearer:

answers = ["yes", "", "no"]

for answer in answers:
    if answer != "":
        print(f"Recorded: {answer}")

Use continue when skipping early lets the useful work remain easy to see, not merely because the keyword exists and looks important. Python has enough drama already.

Ask again after an unusable answer

Input gives continue a practical job. You cannot know how many attempts a person will need, so use while True. Reject an unusable answer with continue, and leave with the already-known break when the answer is good:

options = ["yes", "no"]

while True:
    answer = input("Please answer yes or no: ").strip().lower()

    if answer not in options:
        print(f"Sorry, {answer} is not an option. Try again.")
        continue

    break

print(f"Thank you. You answered {answer}.")

Notice the order. The question updates answer before continue can run. A bad answer prints help and jumps back to the question. A good answer passes the if, reaches break, and leaves the loop.

Cleaning happens as the answer arrives. Someone who YES still meant yes; making them wrestle a space character is not much of a lesson.

Keep progress above continue

In a while loop, Python does not move anything for you. If a continue jumps over the only update, the loop can get stuck:

count = 0

while count < 3:
    if count == 1:
        continue
    count += 1

When count becomes 1, continue skips the update. The next pass sees the same value and skips it again forever.

The input loop avoids that trap because input() assigns a fresh answer before the decision to continue. When you use continue in any while loop, check that the controlling progress changes first.

What does this code print?

for value in [2, 0, 4]:
    if value == 0:
        continue
    print(value)

What does .strip().lower() do for the validation loop?

Now build the same friendly ask-again path yourself.

Task

Write a program that keeps asking for a day preference until it gets a usable answer, and reports how many times it had to ask.

Complete the program with a while True so that:

  • each question uses the prompt Weekday or weekend? and cleans the answer with .strip().lower();

  • attempts counts every answer given, including the unusable ones;

  • an answer not in days reaches continue and asks again;

  • a usable answer reaches break;

  • the two report lines run once after the loop.

Expected final lines:

You chose: weekend
Attempts: 3

When you run it yourself, type a wrong answer or two first and watch the program ask again. Submit runs your program with its own prepared answers, so the exact counts above come from those, not from what you typed.