{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW07 InClass - Repetition and Programming " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following programming tasks in the class\n", "\n", "- generate random numbers until the number 999 comes. Random numbers from 1 up to and including 999\n", "- Calculate the checksum of a number (not recursively), use log for the number\n", "- vignette with dictionary\n", "- Generate password with random\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Find random number \n", "\n", "Write a Python program that generates random numbers from 100 to 1000 until the number 1000 is found. At the end, output to the console how many times you had to run through the loop\n", "\n", "Option: Wrap this program in the function ```wait_till_number(nbr:int)->int```, which takes the \"search number\" as parameter and returns the number of loop iterations until found." ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of loops: 166\n" ] } ], "source": [ "import random\n", "counter = 0\n", "\n", "def wtn(n:int)->int:\n", " global counter\n", " for _ in range(100,1001):\n", " counter += 1\n", "\n", " if random.randint(100,1000) == n:\n", " return counter\n", "\n", "print(f\"number of loops: {wtn(1000)}\")" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "number of loops: 194\n" ] } ], "source": [ "def wnt2(n:int)->int:\n", " counter2 = 0\n", " \n", " while True:\n", " counter2 += 1\n", " i = random.randint(100,1000)\n", "\n", " if i == n:\n", " counter2 += 1\n", " return counter2\n", " \n", "print(f\"number of loops: {wnt2(1000)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Calculate checksum " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Calculator \n", "\n", "Program a \"the little professor calculator\" that presents random calculations and checks whether the input is correct or incorrect.\n", "Only additions in the integer range 1 up to and including 99 should be checked (including results less than 100).\n", "\n", "Implement the function ```gen_addition(low:int, up:int) -> tuple:``` where the tuple (str, int) returns the two values ​​(\"text of the calculation\", result). e.g. ```(\"76 + 13 = ?\", 89)```\n", "\n", "Then, in a second function ```game(round:int):```, call the above function n times and each time return whether the user input was correct and, if incorrect, what the correct result would be. Make a small statistic of how often the answer was correct and output it at the end." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "74 + 96 = 170\n" ] } ], "source": [ "import random\n", "\n", "low,up = random.randint(1,99), random.randint(1,99)\n", "\n", "def add(low:int, up:int)->tuple:\n", " res = low + up\n", " def game(n_round:int):\n", " ... ...\n", " return res\n", "\n", "print(f\"{low} + {up} = {add(low,up)}\")\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Request Random User List \n", "\n", "The following code is given, which queries an API via an https request and returns the result as JSON. The data converted into a dictionary is output to the console.\n", "\n", "\n", "In a new code block, create the function ```get_random_user_list(n_user:int) -> list:```, which uses the given code in the function and calls **n_user times** and creates and returns a list of tuples from queried, random (first names, last names).\n", "\n", "Calling the function with 3 as argument should print an example like the following list:\n", "\n", "```python\n", "[('Karsten', 'Sundberg'), ('Anni', 'Valli'), ('Pamela', 'Hart')]\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'gender': 'female', 'name': {'title': 'Ms', 'first': 'Izzie', 'last': 'Hopkins'}, 'location': {'street': {'number': 6736, 'name': 'Henry Street'}, 'city': 'Clane', 'state': 'Cork', 'country': 'Ireland', 'postcode': 31376, 'coordinates': {'latitude': '-1.6642', 'longitude': '14.2549'}, 'timezone': {'offset': '+1:00', 'description': 'Brussels, Copenhagen, Madrid, Paris'}}, 'email': 'izzie.hopkins@example.com', 'login': {'uuid': '76a7a441-312f-4688-b436-f197c7413b06', 'username': 'happyzebra175', 'password': 'close-up', 'salt': 'gl7zGG8r', 'md5': 'd61cdcf29a62634fa2fc69d7a6bde4fb', 'sha1': 'b229582a917539388b83c352367ab790536f999e', 'sha256': '65e002fe18a0a38bba26cb64288d1b6fa56ff05ddadba78a1ecabd2bd0908655'}, 'dob': {'date': '1978-12-05T23:23:52.720Z', 'age': 45}, 'registered': {'date': '2011-03-11T04:59:44.328Z', 'age': 13}, 'phone': '041-302-7049', 'cell': '081-305-2722', 'id': {'name': 'PPS', 'value': '1807273T'}, 'picture': {'large': 'https://randomuser.me/api/portraits/women/75.jpg', 'medium': 'https://randomuser.me/api/portraits/med/women/75.jpg', 'thumbnail': 'https://randomuser.me/api/portraits/thumb/women/75.jpg'}, 'nat': 'IE'}]\n" ] } ], "source": [ "# this code is given\n", "import requests\n", "\n", "# URL des Webservices, der zufällige Benutzerinformationen liefert\n", "url = \"https://randomuser.me/api/\"\n", "\n", "# Anfrage an den Webservice senden\n", "response = requests.get(url)\n", "\n", "# Überprüfen, ob die Anfrage erfolgreich war\n", "if response.status_code == 200:\n", " # JSON-Antwort parsen\n", " data = response.json()\n", "else:\n", " print(\"Fehler bei der Anfrage:\", response.status_code)\n", "\n", "print(data[\"results\"])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('Karsten', 'Sundberg'), ('Anni', 'Valli'), ('Pamela', 'Hart')]\n" ] } ], "source": [ "# here your code\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Checkprime function \n", "\n", "Write a function ```def checkprime(number:int)->bool:``` which checks whether a number is prime.\n", "\n", "Test your function with different numbers including **10000019** and **1234567897**\n", "\n", "```\n", "10000019 is a prime: True\n", "1234567897 is a prime: False\n", "```\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000019 is a prime: True\n", "1234567897 is a prime: False\n" ] } ], "source": [ "# your code hier:\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 2 }