Skip to content
Open

Main #10

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions for-loops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -322,7 +380,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down