From bc1c66329a94e4c12b57660b91d63d604f3f71de Mon Sep 17 00:00:00 2001 From: katabasist Date: Sun, 2 Feb 2025 22:57:20 +0000 Subject: [PATCH] completed excercise --- for-loops.ipynb | 120 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 110 insertions(+), 10 deletions(-) diff --git a/for-loops.ipynb b/for-loops.ipynb index d8dc735..1620e29 100644 --- a/for-loops.ipynb +++ b/for-loops.ipynb @@ -22,9 +22,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "neoptolemus\n", + "philoctetes\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -47,11 +57,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Odysseus text text text\n", + "Neoptolemus text text text\n", + "Philoctetes text text text\n" + ] + } + ], "source": [ - "# Hint: Use print(f\"{variable}\")" + "# Hint: Use print(f\"{variable}\")\n", + "for character in cast:\n", + " print(character.title() + \" text text text\")" ] }, { @@ -63,9 +85,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "odysseus\n", + "neoptolemus\n", + "philoctetes\n", + "And now the loop is done!\n" + ] + } + ], "source": [ "cast = [\"odysseus\", \"neoptolemus\", \"philoctetes\"]\n", "\n", @@ -137,7 +170,7 @@ "for character in cast:\n", " print(character)\n", "\n", - "print(f\"{character} was played by a great actor!\")" + "print(f\"{character} was played by a great actor!\")\n" ] }, { @@ -304,11 +337,78 @@ "\n", "Matthes 2023, p. 67, Exercise 4-13." ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "toast\n", + "bagels\n", + "waffles\n", + "eggs\n", + "pancakes\n" + ] + } + ], + "source": [ + "buffet = (\"toast\", \"bagels\", \"waffles\", \"eggs\", \"pancakes\")\n", + "for food in buffet:\n", + " print(food)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mbuffet\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmuffins\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "buffet[0] = \"muffins\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "toast\n", + "oatmeal\n", + "waffles\n", + "cereal\n", + "pancakes\n" + ] + } + ], + "source": [ + "buffet = (\"toast\", \"oatmeal\", \"waffles\", \"cereal\", \"pancakes\")\n", + "for food in buffet:\n", + " print(food)" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -322,7 +422,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.1" } }, "nbformat": 4,