diff --git a/for-loops.ipynb b/for-loops.ipynb index d8dc735..2950572 100644 --- a/for-loops.ipynb +++ b/for-loops.ipynb @@ -304,11 +304,69 @@ "\n", "Matthes 2023, p. 67, Exercise 4-13." ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('fries', 'noodles', 'burgers', 'salad', 'wings')\n", + "fries\n", + "noodles\n", + "burgers\n", + "salad\n", + "wings\n" + ] + }, + { + "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[28], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(food)\n\u001b[1;32m 12\u001b[0m \u001b[38;5;66;03m#Try to modify one of the items and get rejected\u001b[39;00m\n\u001b[0;32m---> 13\u001b[0m \u001b[43mfoods\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfries\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtacos\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(foods)\n\u001b[1;32m 16\u001b[0m \u001b[38;5;66;03m#Menu change replacing two items\u001b[39;00m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "#Exercises 4-13 in Matthes 2023, p. 67\n", + "\n", + "#Storing five basic foods in a tuple\n", + "foods = (\"fries\", \"noodles\", \"burgers\", \"salad\", \"wings\")\n", + "print(foods)\n", + "\n", + "\n", + "#For loop to print each restaurant's offerings\n", + "for food in foods:\n", + " print(food)\n", + "\n", + "#Try to modify one of the items and get rejected\n", + "foods[\"fries\"] = \"tacos\"\n", + "print(foods)\n", + "\n", + "#Menu change replacing two items\n", + "foods = (\"fries\", \"noodles\", \"burgers\", \"salad\", \"wings\")\n", + "print(\"\\nOriginal menu:\")\n", + "for food in foods:\n", + " print(food)\n", + "\n", + "foods = (\"fries\", \"chicken\", \"burgers\", \"salad\", \"dips\")\n", + "print(\"\\nModified menu:\")\n", + "for food in foods:\n", + " print(food)\n", + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -322,7 +380,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.12.1" } }, "nbformat": 4,