do-while loops
Programming task — enter your code and have it checked. There are two types of exercises: For most you read values input() a — the check feeds in fixed inputs and compares your output character by character, so give
only what is required. The input prompt (input("…")) hides the exam, so it doesn't bother you. For the remaining exercises (part 1 and part 6)
you define the values yourself in the code — there will be input() not needed and the check looks at your variables directly.
Part 1 — Block runs exactly once
The simplest do-while case: a block that runs once.
Exercise
- Create a variable yourself
wortwith any text (string) - this is where you read nothing there. - Build a do-while structure that
wortexactly once outputs and then ends. Python doesn't know do-while: you start an endless loop, type in the blockwortand breaks off the loop again in the same run.
This is about the construct itself: the block should exactly once run. Give wort only once.
Part 1 — Your Code
Loading Python…Tests
Part 2 — Counting up to a target value
The condition is at the end of the block - the block also runs when n = 1.
Exercise
- Read a target value as an integer (
int(input())); the target value applies ≥ 1. - Start with a counter with the value 1 and print it on a separate line in each iteration of the loop.
- Test at the end of the block whether the counter has reached the target value (counter ≥ Target value): If yes, end the loop, otherwise increase the counter by 1 and continue.
The lines appear at target value 4 1, 2, 3, 4; with target value 1 only the line 1.
Part 2 — Your Code
Loading Python…Tests
Part 3 — Read until “ok” is entered
Real input check: read first, then cancel at the end.
Exercise
- Read a word in a do-while loop (i.e. an endless loop with termination at the end of the block).
input()and output it immediately on a separate line. - Test then (at the end of the block): is the word exactly
ok, end the loop — otherwise read the next word.
Even if it's the first word ok is, it is read once and output. Input test, ok So results in the lines test and ok.
Part 3 — Your Code
Loading Python…Tests
Part 4 — Collect points up to the threshold
Add up the values and check against a limit at the end of the block.
Exercise
- Read one first Threshold as an integer (
int(input())). - Start with a score of 0. Read one at a time in a do-while loop
Growth a (
int(input())), add it to the score and give the new one Points on its own line. - Test then (at the end of the block): the score has reached the threshold (score ≥ Threshold), end the loop — otherwise read the next increment.
At threshold 3 and the increases 1, 1, 1 the lines appear 1, 2, 3. Even at threshold 0 If the block runs once: an increase 5 results in the line 5.
Part 4 — Your Code
Loading Python…Tests
Part 5 — Mini menu until “end”
Read the selection, react, and at the end check for cancellation.
Exercise
- In a do-while loop, read one Selection as an integer (
int(input())). - Is the selection
1, give exactlyStartand read the next selection. - Is the selection
2, give exactlyEndeand then end the loop.
Will be immediately 2 entered, only appears Ende — the block still runs once. Input 1, 2 yields Start and below Ende.
Part 5 — Your Code
Loading Python…Tests
Part 6 — Password-experiments count
Run over a self-defined list - end at the correct password.
Exercise
- Create your own list
eingabenusing password attempts (strings) — this last Element must1234with any incorrect attempts before it. In this task, do nothing withinput()there. - Iterate over the list using
while True:. In each iteration, process the next entry first, then check whether it is exactly"1234". Exit the loop at the first match usingbreak. - Count every checked entry, including the correct password. You may use a counter or a list index. An index starts at 0, so the number of checked entries is one greater than the index.
- Print exactly one number: the number of attempts up to and including
"1234". You may print this immediately beforebreakor after the loop. Do not print anything else.
Standing 1234 already in the first place, is the output 1 (the block still runs once). At ["0000", "1234"] is the output 2.
For ["2222", "1111", "3333", "1234"] the output is 4. The search is for the exact string "1234" rather than the number 1234 or part of a longer password.
Part 6 — Your Code
Loading Python…Tests
Report as PDF
Document and download your results.
Module overview
| Part | Status |
|---|---|
| Part 1: Produce output once | Open |
| Part 2: Counting up to n | Open |
| Part 3: Read until “ok” | Open |
| Part 4: Collect points | Open |
| Part 5: Menu to end | Open |
| Part 6: Password-experiments | Open |
Not all parts are completed yet - you can still create the report, which will then mark which parts are open.
Download PDF
Enter your name and download the report as a PDF.