Combined exercises
Programming task — enter your code and have it checked. Most parts read values input() a; the check then feeds fixed inputs and compares your output character by character — give there only what is required (no additional texts). You read two parts (3 and 6). not but define the function and choose the values to try it out yourself; There the runner checks the function directly. Like input()
works, is stated in the instructions and in the “Variables” module.
Part 1 — Count to n
function with while-Loop and return.
Exercise
Lis n as an integer int(input()) there.
- Write a function above
zaehle_bis(n)that come with onewhile-Loop a text of the numbers 1 to n builds up - the numbers depending on a Space separated — and returns the result. - Call the function with the read in
nand output its return value. - For n = 3 the output is exactly
1 2 3— no spaces at the beginning or end, no additional text.
Part 1 — Your Code
Loading Python…Tests
Part 2 — Even/Odd
for-Loop through a list, using if decide.
Exercise
Read the number first n with int(input()) one and then in a loop the n Numbers - one of each int(input()) — into a list zahlen.
- Write a function
pruefe_zahlen(zahlen)that come with onefor-Loop checks each number to see if it is even is (remainder 0 when divided by 2), and puts a suitable text in a result list for each number: for an even number<zahl> ist gerade, otherwise<zahl> ist ungerade(with the respective number instead<zahl>). Return the list. - In the main block: call function and each Output the text of the return list on a separate line.
- Example: input
4, then1 2 3 4results in four lines1 ist ungerade/2 ist gerade/3 ist ungerade/4 ist gerade.
Part 2 — Your Code
Loading Python…Tests
Part 3 — Welcome
if/else within a function - here the function itself is the focus.
Exercise
Here will nothing read in: You define the function and choose the values to try out yourself. The runner then checks the function directly.
- Write a function
begruessung(alter): That's italterunder 18, it should return the textHallo Kindreturn, otherwiseHallo Erwachsener. Exactly 18 is already considered an adult. - Call the function in the block
if __name__ == "__main__":twice twice: once with an age below 18 and once with an age of at least 18. Output both return values. - This is how you make both branches visible: once
Hallo Kind, onceHallo Erwachsener.
Part 3 — Your Code
Loading Python…Tests
Part 4 — Sum to n
Accumulator in a function with return.
Exercise
Lis n as an integer int(input()) there.
- Write a function
summe_bis(n)that come with onewhile-Loop the sum of all numbers from 1 to n added up and returns the result. - Call the function with the read in
nup and give only the number returned. - For n = 5 is the output
15, at n = 10 then55.
Part 4 — Your Code
Loading Python…Tests
Part 5 — Evaluate grades
for-loop + if/elif within a function.
Exercise
Read the number first n with int(input()) one and then in a loop the n Notes — one each int(input()) — into a list noten.
- Write a function
zeige_noten(noten)that come with onefor-loop andif/eliftranslates each note into exactly this text and returns the texts as a list:1 → sehr gut,2 → gut,3 → befriedigend,4 → ausreichend, everything else (e.g. 5) →nicht bestanden. - In the main block: call function and each Output the text of the return list on a separate line.
- Example: input
3, then1 3 5results in the three linessehr gut/befriedigend/nicht bestanden. Pay attention to the exact spelling of the texts.
Part 5 — Your Code
Loading Python…Tests
Part 6 — Login with three attempts
Function with two return values — the focus here is on the tuple return.
Exercise
Here too nothing read in: You define the function and choose the passwords to try it out yourself. The runner then checks the function directly.
- Write a function
login_test(passwort)with onewhileloop for a maximum of 3 attempts. The attempt number starts at 1. If the password matches"1234"match, the function should two Return values: the textLogin okand the current attempt number. If the password is still incorrect after 3 iterations, the function returns the textFalsches Passworttogether with the number 3 back. - Call the function in the main block twice on — once with
"1234", once with a wrong password — and unpack it two Returns in each casetextandanzahl_versuche. - Give each first
text(line 1), thenanzahl_versuche(line 2). With"1234"appearsLogin ok/1, with an incorrect passwordFalsches Passwort/3.
Note: The password is passed here once as a parameter (no input() per experiment). The whileThe loop therefore primarily demonstrates incrementing the attempt counter and returning early when the login succeeds. Tip: In Python, a function can return multiple comma-separated values.
Part 6 — Your Code
Loading Python…Tests
Part 7 — Using square multiple times
Call the same function for multiple values.
Exercise
Read three whole numbers one after the other a, b and c one — one each
int(input()).
- Write a function
quadrat(zahl), which returns the square of its argument — i.e number multiplied by itself. - Call the function for each of the three numbers and output the three squares — each on its own line, in the order in which it was entered.
- Example: input
2,5,8results in the three lines4/25/64.
Part 7 — Your Code
Loading Python…Tests
Report as PDF
Document and download your results.
Module overview
| Part | Status |
|---|---|
| Part 1: Count to n | Open |
| Part 2: Even/Odd | Open |
| Part 3: Welcome | Open |
| Part 4: Sum to n | Open |
| Part 5: Notes | Open |
| Part 6: Login | Open |
| Part 7: Square | 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.