no way to compare when less than two revisions
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| — | de:modul:m319:learningunits:lu09:loesungen:funktionen [2025/06/23 07:45] (aktuell) – ↷ Seite von modul:m319:learningunits:lu09:loesungen:funktionen nach de:modul:m319:learningunits:lu09:loesungen:funktionen verschoben msuter | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== LU09.L02 - Inhalte in Methoden auslagern ====== | ||
| + | |||
| + | ==== 1. Menüauswahl in Methode auslagern ==== | ||
| + | Wir haben uns vorgenommen, | ||
| + | |||
| + | <code java> | ||
| + | # Show Menue | ||
| + | print() | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | </ | ||
| + | |||
| + | Wir möchten diesen Programmteil nun in eine externe Methode packen. Wir arbeiten uns also durch unsere 3 Schritte durch: | ||
| + | |||
| + | === 1. Funktionsblock definieren === | ||
| + | |||
| + | * **Name**: Die Methode druckt die Menüauswahl auf die Konsole aus, mögliche Namen wären z.B '' | ||
| + | * **Parameter**: | ||
| + | * **Return**: Die Methode gibt keinen Wert zurück, ist also '' | ||
| + | |||
| + | <code python> | ||
| + | |||
| + | === 2. docstring erstellen === | ||
| + | Ein kurzer beschreibender Satz. | ||
| + | <code python> | ||
| + | def print_menue(): | ||
| + | """ | ||
| + | Prints the menue to the user | ||
| + | :return: None | ||
| + | """ | ||
| + | </ | ||
| + | |||
| + | === 3. Logik Implementieren === | ||
| + | |||
| + | <file python main.py> | ||
| + | def print_menue(): | ||
| + | """ | ||
| + | Prints the menue to the user | ||
| + | :return: None | ||
| + | """ | ||
| + | print() | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | def talk_to_user(): | ||
| + | """ | ||
| + | Talks to the user to determine what he wants. | ||
| + | |||
| + | :return: None | ||
| + | """ | ||
| + | |||
| + | print(' | ||
| + | |||
| + | # Show Menue | ||
| + | print_menue() | ||
| + | selection = input('>> | ||
| + | |||
| + | while selection != ' | ||
| + | if selection == ' | ||
| + | print(' | ||
| + | position = int(input('' | ||
| + | number1 = 0 | ||
| + | number2 = 1 | ||
| + | counter = 3 | ||
| + | while counter <= position: | ||
| + | next_number = number1 + number2 | ||
| + | number1 = number2 | ||
| + | number2 = next_number | ||
| + | counter += 1 | ||
| + | |||
| + | print(number2) | ||
| + | elif selection == ' | ||
| + | print(' | ||
| + | number = int(input('' | ||
| + | for counter in range(0, 11): | ||
| + | print(number * counter) | ||
| + | elif selection == ' | ||
| + | print(' | ||
| + | number = int(input(' | ||
| + | if number % 2 == 0: | ||
| + | print(f' | ||
| + | else: | ||
| + | print(f' | ||
| + | else: # Wrong input | ||
| + | print(' | ||
| + | |||
| + | print_menue() | ||
| + | selection = input('>> | ||
| + | |||
| + | |||
| + | if __name__ == ' | ||
| + | talk_to_user() | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 2. Unterprogramme auslagern ==== | ||
| + | Um die Übersichtlichkeit des Codes zu erhöhen, lagern wir die Unterprogramme () in eigene Methoden aus. | ||
| + | |||
| + | |||
| + | === 1. Funktionsblock definieren === | ||
| + | |||
| + | | ^ Name ^ Parameter | ||
| + | ^ Fibonacci | ||
| + | ^ Einmaleins | ||
| + | ^ Gerade/ | ||
| + | |||
| + | |||
| + | === 2. docstring erstellen === | ||
| + | |||
| + | | ^ docstring | ||
| + | ^ Fibonacci | ||
| + | ^ Einmaleins | ||
| + | ^ Gerade/ | ||
| + | |||
| + | === 3. Logik implementieren === | ||
| + | <file python main.py> | ||
| + | |||
| + | def print_menue(): | ||
| + | """ | ||
| + | Prints the menue to the user | ||
| + | :return: None | ||
| + | """ | ||
| + | print() | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | |||
| + | def fibonacci(): | ||
| + | """ | ||
| + | Prints the value from the Fibonacci series at position x. | ||
| + | :return: None | ||
| + | """ | ||
| + | print(' | ||
| + | position = int(input('' | ||
| + | number1 = 0 | ||
| + | number2 = 1 | ||
| + | counter = 3 | ||
| + | while counter <= position: | ||
| + | next_number = number1 + number2 | ||
| + | number1 = number2 | ||
| + | number2 = next_number | ||
| + | counter += 1 | ||
| + | |||
| + | print(number2) | ||
| + | |||
| + | |||
| + | def multiplication_table(): | ||
| + | """ | ||
| + | Prints the multiplication table for the number x. | ||
| + | :return: | ||
| + | """ | ||
| + | print(' | ||
| + | number = int(input('' | ||
| + | for counter in range(0, 11): | ||
| + | print(number * counter) | ||
| + | | ||
| + | |||
| + | def even_or_odd(): | ||
| + | """ | ||
| + | Prints if the value is even or odd | ||
| + | : | ||
| + | """ | ||
| + | print(' | ||
| + | number = int(input(' | ||
| + | if number % 2 == 0: | ||
| + | print(f' | ||
| + | else: | ||
| + | print(f' | ||
| + | |||
| + | |||
| + | def talk_to_user(): | ||
| + | """ | ||
| + | Talks to the user to determine what he wants. | ||
| + | |||
| + | :return: None | ||
| + | """ | ||
| + | |||
| + | print(' | ||
| + | |||
| + | # Show Menue | ||
| + | print_menue() | ||
| + | selection = input('>> | ||
| + | |||
| + | while selection != ' | ||
| + | if selection == ' | ||
| + | fibonacci() | ||
| + | elif selection == ' | ||
| + | multiplication_table() | ||
| + | elif selection == ' | ||
| + | even_or_odd() | ||
| + | else: # Wrong input | ||
| + | print(' | ||
| + | |||
| + | print_menue() | ||
| + | selection = input('>> | ||
| + | |||
| + | |||
| + | if __name__ == ' | ||
| + | talk_to_user() | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | [[https:// | ||