while loops
Parts 1–4 and part 6 read input using input() You can enter values yourself when running the code; the tests use predefined inputs. Output only the requested results. In part 5, choose the upper limit yourself.
Part 1 — Count up to n
A simple counting loop with an upper limit read in.
Exercise
- Read a whole number
na (int(input())). - Write one
while-Loop that starts with a counting variable1starts and all numbers from1tonwithprint()outputs — each on its own line, in ascending order.
When entering 5 so should 1, 2, 3,
4, 5 (one line each). Only output these numbers.
Part 1 — Your Code
Loading Python…Tests
Part 2 — Count backwards from n
The other direction — how does the condition change?
Exercise
- Read a whole number
na (int(input())). - Write one
while-Loop that starts with a counting variablenstarts and all numbers fromndown to0outputs — each on its own line, in descending order.
When entering 3 so should 3, 2, 1,
0 (one line each). Only output these numbers.
Part 2 — Your Code
Loading Python…Tests
Part 3 — Sum from 1 to n
Battery batteries.
Exercise
- Read a whole number
na (int(input())). - Set a counting variable (Start
1) and a sum variable (Start0). - Write one
while-Loop that runs as long as the counting variable<= nis. In the block, add the counting variable to the sum and then increase the counting variable by 1. - Pass after the loop only the sum with
print()out.
When entering 5 is the sum 1+2+3+4+5 = 15, at 10 it is
55. Just output that one number.
Part 3 — Your Code
Loading Python…Tests
Part 4 — Doubling
Loop with variable condition.
Exercise
- Read two positive integers: first a Date, then one Border (two
int(input())). - Write one
while-Loop with a counting variable that starts at the start value and as long as runs as it<=the limit is. - Output the counting variable in each run (separate line) and then multiply it by 2, so it doubles with each run.
At start value 2 and border 50 results in that 2, 4,
8, 16, 32 (one per line). Once the value exceeds the limit, do not output it. Output only these numbers.
If the starting value equals the limit, print it exactly once. If the starting value exceeds the limit, print nothing.
Part 4 — Your Code
Loading Python…Tests
Part 5 — Even Number List
Step size ≠ 1 — build a list yourself.
Exercise
Here you read nothing but set the values yourself.
- Choose one yourself even Upper limit and assign it to a variable
nto (e.g. B. the value 10). - Build with one
while-Loop a list with the namegeradeon that all even numbers of2up to (including)ncontains. Idea: Start at2, append the current counter to the list and then increment it each time2. - Give the finished list
geradeat the end.
At the upper limit 10 arises e.g. B. the output [2, 4, 6, 8, 10]. Which even number you choose is up to you - it is checked that the list is constructed and output correctly.
Part 5 — Your Code
Loading Python…Tests
Part 6 — Check a password
Read input, compare it and ask again after an incorrect attempt.
Exercise
- Set the predefined password using
PWD = "1234"as the assignment. - Read along
input()to read a password attempt. Compare the input as a string withPWD. - While the input is incorrect, output
Falsches Passwortand ask for the password again inside awhileloop. - Once the password is correct, the loop ends. Then output the following once:
Login okout.
When you run the code, an input dialog opens for each attempt. Try an incorrect password first, then the correct one. If the first input is correct, no error message appears.
Part 6 — Your Code
Loading Python…Tests
Report as PDF
Document and download your results.
Module overview
| Part | Status |
|---|---|
| Part 1: Count up | Open |
| Part 2: Backwards | Open |
| Part 3: Total | Open |
| Part 4: Doubling | Open |
| Part 5: Even Number List | Open |
| Part 6: Password | 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.