From e750245675ff3133e332f56a289d2340ec6345ff Mon Sep 17 00:00:00 2001 From: Jonas_Jones <91549607+J-onasJones@users.noreply.github.com> Date: Fri, 10 Nov 2023 02:17:08 +0100 Subject: [PATCH] added ue03 --- .gitignore | 3 + EPR/ue03/README.md.txt | 35 +++++++++++ EPR/ue03/epr_functions.py | 119 ++++++++++++++++++++++++++++++++++++++ EPR/ue03/main.py | 36 ++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 EPR/ue03/README.md.txt create mode 100644 EPR/ue03/epr_functions.py create mode 100644 EPR/ue03/main.py diff --git a/.gitignore b/.gitignore index 9d34d8b..6714baa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ +__pycache__ + /EPR/ue00/7987847_epr-blatt00.zip /EPR/ue01/7987847_epr-blatt01.zip /EPR/ue02/7987847_epr-blatt02.zip +/EPR/ue03/7987847_epr-blatt03.zip /GPR/ue03/gpr_blatt03_7987847_werner.zip \ No newline at end of file diff --git a/EPR/ue03/README.md.txt b/EPR/ue03/README.md.txt new file mode 100644 index 0000000..ba54f0f --- /dev/null +++ b/EPR/ue03/README.md.txt @@ -0,0 +1,35 @@ +author: 7987847, Werner + +Das Programm ist eine Konsole für das epr_functions Modul. + +Zur Nutzung muss eine kompatible Python version installiert sein. +Akzeptiert werden alle Python 3.x Versionen. + +Das Programm kann mit dem Befehl `python3 main.py` im Ordner gestartet werden. + +Beim Starten wird dem Nutzer einen Tipp gegeben, wie eine Liste der Befehle angezeigt werden kann und wie das Programm beendet werden kann. + +## Befehle + +### `help` + +Zeigt eine Liste aller Befehle an. + +### `exit` + +Beendet das Programm. + +### `decimal_to_octal` + +Konvertiert eine Dezimalzahl in eine Oktalzahl. +Nimmt ein Argument entgegen: Die Dezimalzahl. + +### `decimal_to_basis` + +Konvertiert eine Dezimalzahl in eine Zahl mit einer beliebigen Basis. +Nimmt zwei Argumente entgegen: Die Dezimalzahl und die Basis. + +### `chaos_turtle` + +Zeichnet eine Dreieck-Struktur mit turtle-graphics. +Nimmt ein Argument entgegen: Die Anzahl der Ecken. \ No newline at end of file diff --git a/EPR/ue03/epr_functions.py b/EPR/ue03/epr_functions.py new file mode 100644 index 0000000..464d492 --- /dev/null +++ b/EPR/ue03/epr_functions.py @@ -0,0 +1,119 @@ +'''EPR Übungsblatt 03, Python Modul''' +__author__ = "7987847, Werner" + +import turtle +import random + +class InvalidInputError(Exception): + '''Exception for invalid input''' + pass + +def decimal_to_octal(decimal:int) -> str: + '''Converts a decimal number to an octal number + decimal: decimal number + ''' + if decimal == 0: + return "0" + if decimal < 0: + raise InvalidInputError("Negative numbers are not allowed") + octal = "" + while decimal > 0: + octal = str(decimal % 8) + octal + decimal //= 8 + print("Octal Conversion:",decimal*8, + "/ 8 =", decimal, + " Rest:", decimal % 8, + "Octal: ", octal) + return octal + +# Alternativ: +# def decimal_to_octal(decimal:int) -> str: +# '''Converts a decimal number to an octal number +# decimal: decimal number +# ''' +# return decimal_to_basis(decimal, 8) + +# Testfälle + +# decimal_to_octal(0) +# Ausgabe: 0 + +# decimal_to_octal(39) +# Ausgabe: 47 + +# decimal_to_octal(-10) +# Ausgabe: Exception + +def decimal_to_basis(decimal:int, basis:int) -> str: + '''Converts a decimal number to a number with a given base + decimal: decimal number + basis: base + ''' + if decimal == 0: + return "0" + if decimal < 0 or basis < 0: + raise InvalidInputError("Negative numbers are not allowed") + converted = "" + while decimal != 0: + converted = str(decimal % basis) + converted + decimal //= basis + print("Basis Conversion:", decimal*basis, "/", + basis, "=", decimal, + "Rest:", decimal % basis, + "Converted:", converted) + return converted + +# Testfälle + +# decimal_to_basis(0, 2) +# Ausgabe: 0 + +# decimal_to_basis(39, 2) +# Ausgabe: 100111 + +# decimal_to_basis(-10, 2) +# Ausgabe: Exception + +def chaos_turtle(iterationen, startpunkt=(0, 0)): + '''Draws a random triangle-like pattern with turtle + iterationen: number of iterations + startpunkt: start point of the turtle + ''' + turtle.penup() + turtle.goto(startpunkt) + turtle.speed(0) + + punkte = [(100, -50), (-100, -50), (0, 100)] # Eckpunkte des Dreiecks + for _ in range(iterationen): + zufallspunkt = random.choice(punkte) + ziel_x = (zufallspunkt[0] + turtle.xcor()) / 2 + ziel_y = (zufallspunkt[1] + turtle.ycor()) / 2 + turtle.goto(ziel_x, ziel_y) + turtle.dot(5) # Punkt zeichnen + +# Testfälle + +# zeichne_dreieck(1000, (0, 0)) +# Ausgabe: Dreieck mit 1000 Punkten + +# zeichne_dreieck(1000, (100, 100)) +# Ausgabe: Dreieck mit 1000 Punkten, verschoben um (100, 100) + +# zeichne_dreieck(1000, (-100, -100)) +# Ausgabe: Dreieck mit 1000 Punkten, verschoben um (-100, -100) + +if __name__ == "__main__": + print("Der Rechner rechnet Dezimalzahlen in Oktalzahlen um.") + print("Geben Sie eine Dezimalzahl ein.") + print(decimal_to_octal(int(input("Dezimalzahl: ")))) + + print("Der Rechner rechnet Dezimalzahlen in Zahlen mit einer beliebigen Basis um.") + print("Geben Sie eine Dezimalzahl und eine Basis ein.") + print(decimal_to_basis(int(input("Decimal: ")), int(input("Basis: ")))) + + print("Der Rechner zeichnet ein Chaos-Turtle.") + print("In einem Dreieck-pattern werden Punkte gezeichnet.") + print("Geben Sie die Anzahl der Iterationen ein.") + chaos_turtle(int(input("Interationen: ")), + (int(input("Startpunkt X Kooridnate: ")), int(input("Startpunkt Y Kooridnate: ")))) + input("Press Enter to close window...") diff --git a/EPR/ue03/main.py b/EPR/ue03/main.py new file mode 100644 index 0000000..c6f8c89 --- /dev/null +++ b/EPR/ue03/main.py @@ -0,0 +1,36 @@ +'''EPR Übungsblatt 03''' +__author__ = "7987847, Werner" + +import epr_functions + +print("Welcome to the EPR Functions Console.") +print("Type 'help' for a list of commands.") +print("Type 'exit' to exit the console.") + +# Main loop of the console +while True: + command = input("> ") + try: + + if command == "help": + print("Commands:") + print("help - Shows this help") + print("exit - Exits the console") + print("decimal_to_octal - Converts a decimal number to an octal number") + print("decimal_to_basis - Converts a decimal number to a number with a given base") + print("chaos_turtle - Draws a random pattern with a turtle") + elif command == "exit": + break + elif command == "decimal_to_octal": + print("Der Rechner rechnet Dezimalzahlen in Oktalzahlen um.") + print("Geben Sie eine positive Dezimalzahl ein.") + print(epr_functions.decimal_to_octal(int(input("Dezimalzahl: ")))) + elif command == "decimal_to_basis": + print("Der Rechner rechnet Dezimalzahlen in Zahlen mit einer beliebigen Basis um.") + print("Geben Sie eine positive Dezimalzahl und eine positive Basis ein.") + print(epr_functions.decimal_to_basis(int(input("Dezimalzahl: ")), int(input("Basis: ")))) + else: + print("Unknown command. Type 'help' for a list of commands.") + # Catch ValueError exceptions, when the user enters invalid input + except epr_functions.InvalidInputError: + print("Invalid input. Please try again.")