for loops
Programming task — write your code yourself and have it checked. The editor is empty; you write everything, including the reading. There are two types of parts: Most
you read the values yourself input() a — the check feeds in fixed inputs and compares your output character by character, so give only what is required. With the other parts you define the data yourself (e.g. B. your own list) and follow along for above - there the test directly checks your data structure and output.
Part 1 — Numbers 1 to n
The classic counting loop with range().
Exercise
- Read a whole number first
int(input())there. - Run with one
for-Loop over the numbers from 1 up to the read number (inclusive) and output each number on its own line. Remember that the top end atrange()is exclusive.
Example: input 5 → the lines 1, 2, 3,
4, 5. At 3 only until 3.
Part 1 — Your Code
Loading Python…Tests
Part 2 — List of names
Create your own list and iterate over it.
Exercise
- Define yourself a list
namenwith three names (Strings) — you choose the names freely. Here will nothing read in. - Go with one
for-Loop through the list and print each name on its own line.
The test checks that namen is a list of three strings and that your output contains exactly these names - each on its own line, in list order.
Part 2 — Your Code
Loading Python…Tests
Part 3 — Sum of four numbers
Accumulator pattern in a for loop.
Exercise
- Start with a sum of 0. Then run with one
for-Small exactly four times and read an integer in each runint(input())one (a total of fourinput()-views). - In the loop block, add each number read in to the running total.
- Give in the end only the sum.
Example: inputs 2, 4, 6, 8 → Output 20.
Part 3 — Your Code
Loading Python…Tests
Part 4 — Even numbers up to n
range() with step size.
Exercise
- Read a whole number first
int(input())there. - Give with one
for-Loop only that even Numbers from 2 up to and including the read number - each on its own line. Tip:range()takes a step size as the third parameter with which you can skip every second number.
Example: input 10 → the lines 2, 4, 6,
8, 10. At 6 only until 6.
Part 4 — Your Code
Loading Python…Tests
Part 5 — Count letters
Iterate over a string.
Exercise
- Read along
input()one after the other first that Word and then that Letters (in that order) and start with a counter of 0. - Run with one
for-Loop over the characters of the word and increase the counter by 1 whenever the current character is the same as the letter you are looking for. - Give in the end only the number.
Example: word Banane, letter a → Output 2. The comparison is case-sensitive — for words Apfel and letter a is the result 0 (the big one A doesn't count).
Part 5 — Your Code
Loading Python…Tests
Part 6 — Even or Odd?
Run over your own list and classify each item.
Exercise
- Define yourself a list
zahlenwith at least three integers — you choose them freely. Here will nothing read in. - Give with one
for-Loop exactly one line for each number:<zahl> ist geradeand.<zahl> ist ungerade. You can tell whether a number is even by looking at the remainder when divided by 2 (modulo operator%): if the remainder is 0, it is even.
Example: At zahlen = [1, 2, 3, 4, 5] the test expects the lines
1 ist ungerade, 2 ist gerade, 3 ist ungerade,
4 ist gerade, 5 ist ungerade. Pay close attention to the text “is even” or “is odd”.
Part 6 — Your Code
Loading Python…Tests
Report as PDF
Document and download your results.
Module overview
| Part | Status |
|---|---|
| Part 1: 1 to n | Open |
| Part 2: Names | Open |
| Part 3: Total | Open |
| Part 4: Even numbers | Open |
| Part 5: Counting letters | Open |
| Part 6: Even/Odd | 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.