From bb1a9dc1eb9ff8d2164d6836b84f5da08e72753b Mon Sep 17 00:00:00 2001 From: luay
Date: Wed, 3 Jun 2026 14:06:26 +0200 Subject: [PATCH] =?UTF-8?q?assignment=20Ordner=20eingef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +++ assignments/test.py | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 assignments/test.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8f40d37 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "workbench.activityBar.autoHide": true +} \ No newline at end of file diff --git a/assignments/test.py b/assignments/test.py new file mode 100644 index 0000000..53da95e --- /dev/null +++ b/assignments/test.py @@ -0,0 +1,54 @@ +import requests + + +def get_advice_by_id(advice_id): + # Prepare the API endpoint with the given ID + url = f"https://api.adviceslip.com/advice/{advice_id}" + + try: + # Send HTTP GET Request + response = requests.get(url) + + # Check if the request was successful (HTTP Status 200) + if response.status_code == 200: + data = response.json() + + # Extract the advice if it exists in the JSON payload + if 'slip' in data: + advice_text = data['slip']['advice'] + print(f"\n[Advice #{advice_id}]:") + print(f"\"{advice_text}\"\n") + else: + print( + f"\n[Warning]: Could not find advice with ID {advice_id}.\n") + + else: + print( + f"\n[Error]: Server connection failed. Status Code: {response.status_code}\n") + + # Handle network connection exceptions + except requests.exceptions.RequestException as e: + print(f"\n[Error]: Network problem occurred: {e}\n") + + +# Main execution block +if __name__ == "__main__": + print("--- Advice Fetcher ---") + print("Type 'exit' to quit the application.\n") + + while True: + user_input = input("-> Enter advice ID (e.g., 10): ") + + # Exit condition + if user_input.lower() == 'exit': + print("Goodbye!") + break + + # Validate if the input is a number + if user_input.isdigit(): + get_advice_by_id(user_input) + else: + print("[Warning]: Please enter a valid number only!\n") + print("[Warning]: Please enter a valid number only!\n") + print("[Warning]: Please enter a valid number only!\n") + print("dafa")