Main block (`if __name__ == "main"`)
Programming task — enter your code and have it checked. Follow the pattern throughout: Function above define, in block
if __name__ == "__main__": the function call and the result
output. For most parts you read the values input() and the check compares your output character by character — enter there only what is required. For two parts (4 and 6) you set the values against them yourself in the main block; There we check the behavior of your function directly.
Part 1 — Direct execution and import
Use an existing function and control when the program starts.
Exercise
In the functions module, you wrote hallo(name) yourself. Here, the function is already provided. Your new task is to distinguish direct execution from import.
- Keep the provided function above the main block.
- Below it, add
if __name__ == "__main__":Read a name only inside this block usinginput()and output the return value ofhallo(name)out. - When run directly, the program should output, for example,
Hallo, Max!When imported, the program must not request input or produce output; the function must still be callable.
“Run” executes your program directly. “Check” also tests importing it under a different module name.
Part 1 — Your Code
Loading Python…Tests
Part 2 — Function addiere(a, b)
Read in two numbers, add them, output them.
Exercise
- Write a function
addiere(a, b), which returns the sum of the two parameters. - In the main block: read two in a row integers
aandbone - each withint(input()). - Call the function and return only the sum.
In the inputs 2 and 3 appears 5.
Part 2 — Your Code
Loading Python…Tests
Part 3 — Function zeige_name(name)
Incorporate the read name into a sentence.
Exercise
- Write a function
zeige_name(name), which contains a sentence with the passed name return. - In the main block: read a name
input()there. - Call the function and print the result.
Be specific Der Name ist <name>. off — when entering Lena therefore Der Name ist Lena.
Part 3 — Your Code
Loading Python…Tests
Part 4 — plus_eins(x) and minus_eins(x)
Define two functions - here you set the value yourself, none input().
Exercise
- Write two functions:
plus_eins(x)gives it away one increased value back,minus_eins(x)the around one decreased. - In the main block, set
xyourself to a fixed value (noinput()). - Call both functions and give
plus_eins(x)in Row 1 andminus_eins(x)in Row 2 out.
What counts here is not a fixed output, but rather that your functions for everyone Calculate value correctly: The test is calling plus_eins(...) and minus_eins(...)
even with several numbers (e.g. B. expects them at 5 the values 6
and 4).
Part 4 — Your Code
Loading Python…Tests
Part 5 — begruessung(vorname, nachname)
Combine two entries into one sentence.
Exercise
- Write a function
begruessung(vorname, nachname), which returns a completed greeting sentence. - In the main block: read one after the other First name and Name one (two
input()). - Call the function and print the result.
Be specific Hallo, <vorname> <nachname>! from — at Mia and Schmidt therefore Hallo, Mia Schmidt!
Part 5 — Your Code
Loading Python…Tests
Part 6 — flaeche_und_umfang(breite, hoehe)
Two return values + tuple unpacking — you set values yourself, none input().
Exercise
- Write a function
flaeche_und_umfang(breite, hoehe), the two values returns: first the area, then the perimeter. Separate both values inreturnwith a comma — Python then packs them into a tuple. - In the main block, set Width and Height yourself to fixed integers (no
input()). - Calls
flaeche_und_umfang(...)up and catch them two Returns with tuple unpacking in two variables (two names separated by commas to the left of the=). - Give that Area in line 1 and the Scope in line 2 out.
The returned tuple of your function is checked for several rectangles - the test calls
flaeche_und_umfang(...) itself on (at 4 and 5 expects them
(20, 18): area 20, scope 18). Pay particular attention to the scope: it counts every page with, i.e. twice the width plus twice the height.
Part 6 — Your Code
Loading Python…Tests
Report as PDF
Document and download your results.
Module overview
| Part | Status |
|---|---|
| Part 1: Execution and import | Open |
| Part 2: add | Open |
| Part 3: show_name | Open |
| Part 4: plus_one / minus_one | Open |
| Part 5: greeting | Open |
| Part 6: area_and_perimeter | 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.