{ "cells": [ { "cell_type": "markdown", "id": "42891916-d15d-4044-beb9-f41f7ea28420", "metadata": {}, "source": [ "# 1. Broken Richter Scale\n", "\n", "#### The tests are in the wrong order. They should be reversed so that we test the most specific condition first and test in the order of decreasing specificity." ] }, { "cell_type": "code", "execution_count": 5, "id": "df3d9f7a-38b5-4cec-ba91-9e9076dfb722", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Richter value 0.5: No destruction of buildings\n", "Richter value 1.5: No destruction of buildings\n", "Richter value 2.5: No destruction of buildings\n", "Richter value 3.5: No destruction of buildings\n", "Richter value 4.5: Damage to poorly constructed buildings\n", "Richter value 5.5: Damage to poorly constructed buildings\n", "Richter value 6.5: Damage to poorly constructed buildings\n", "Richter value 7.5: Damage to poorly constructed buildings\n", "Richter value 8.5: Damage to poorly constructed buildings\n", "Richter value 9.5: Damage to poorly constructed buildings\n" ] } ], "source": [ "# TESTS IN THE WRONG ORDER!\n", "\n", "for r in range(0, 10):\n", " richter = r + 0.5\n", " print(f'Richter value {richter:3.1f}: ', end='')\n", "\n", " if richter < 4.5:\n", " print('No destruction of buildings')\n", " elif richter >= 4.5:\n", " print('Damage to poorly constructed buildings')\n", " elif richter >= 6:\n", " print('Many buildings considerably damaged, some collapse')\n", " elif richter >= 7:\n", " print('Many buildings destroyed')\n", " elif richter >= 8:\n", " print('Most structures fall')" ] }, { "cell_type": "code", "execution_count": 7, "id": "b5c6a308-6a58-4e6e-b87d-333fe8bef358", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Richter value 0.5: No destruction of buildings\n", "Richter value 1.5: No destruction of buildings\n", "Richter value 2.5: No destruction of buildings\n", "Richter value 3.5: No destruction of buildings\n", "Richter value 4.5: Damage to poorly constructed buildings\n", "Richter value 5.5: Damage to poorly constructed buildings\n", "Richter value 6.5: Many buildings considerably damaged, some collapse\n", "Richter value 7.5: Many buildings destroyed\n", "Richter value 8.5: Most structures fall\n", "Richter value 9.5: Most structures fall\n" ] } ], "source": [ "# TESTS IN THE CORRECT ORDER!\n", "\n", "for r in range(0, 10):\n", " richter = r + 0.5\n", " \n", " print(f'Richter value {richter:3.1f}: ', end='')\n", "\n", " if richter >= 8:\n", " print('Most structures fall')\n", " elif richter >= 7:\n", " print('Many buildings destroyed')\n", " elif richter >= 6:\n", " print('Many buildings considerably damaged, some collapse')\n", " elif richter >= 4.5:\n", " print('Damage to poorly constructed buildings')\n", " else:\n", " print('No destruction of buildings')" ] }, { "cell_type": "markdown", "id": "1e3c4aad-38b6-4667-9b08-a2936d40b74b", "metadata": {}, "source": [ "#### Another possible solution, one that keeps the tests in the original order. But this code will run slower because the tests are more complicated." ] }, { "cell_type": "code", "execution_count": 6, "id": "ac349570-7943-499b-9121-ca3a8615fefa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Richter value 0.5: No destruction of buildings\n", "Richter value 1.5: No destruction of buildings\n", "Richter value 2.5: No destruction of buildings\n", "Richter value 3.5: No destruction of buildings\n", "Richter value 4.5: Damage to poorly constructed buildings\n", "Richter value 5.5: Damage to poorly constructed buildings\n", "Richter value 6.5: Many buildings considerably damaged, some collapse\n", "Richter value 7.5: Many buildings destroyed\n", "Richter value 8.5: Most structures fall\n", "Richter value 9.5: Most structures fall\n" ] } ], "source": [ "# ALTERNATE SOLUTION\n", "\n", "for r in range(0, 10):\n", " richter = r + 0.5\n", " \n", " print(f'Richter value {richter:3.1f}: ', end='')\n", "\n", " if richter < 4.5:\n", " print('No destruction of buildings')\n", " elif 4.5 <= richter < 6:\n", " print('Damage to poorly constructed buildings')\n", " elif 6 <= richter < 7:\n", " print('Many buildings considerably damaged, some collapse')\n", " elif 7 <= richter < 8:\n", " print('Many buildings destroyed')\n", " else:\n", " print('Most structures fall')" ] }, { "cell_type": "markdown", "id": "0b6a82f9-97cc-4c49-9a04-3a079e5bb7b8", "metadata": {}, "source": [ "# 2. Multiplication table\n", "\n", "#### Create the table with a nested `for` loop. \n", "\n", "#### The outer loop controls variable `row` and iterates once per row. At the end of each iteration, it executes `print()` to generate a line feed.\n", "\n", "#### The inner loop controls variable `col`. For each iteration of the outer `for` loop, the inner `for` loop iterates once per column of a row. Then the value of each \"cell\" of the table is the product `row*col`.\n", "\n", "#### Use an f-string to print each product with four spaces: `f'{row*col:4}'`. Use `end=''` to print the next product of the row in the same line." ] }, { "cell_type": "code", "execution_count": 8, "id": "4bc6d302-0522-44f0-8f25-a0919ad112e4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 2 3 4 5 6 7 8 9 10\n", " 2 4 6 8 10 12 14 16 18 20\n", " 3 6 9 12 15 18 21 24 27 30\n", " 4 8 12 16 20 24 28 32 36 40\n", " 5 10 15 20 25 30 35 40 45 50\n", " 6 12 18 24 30 36 42 48 54 60\n", " 7 14 21 28 35 42 49 56 63 70\n", " 8 16 24 32 40 48 56 64 72 80\n", " 9 18 27 36 45 54 63 72 81 90\n", " 10 20 30 40 50 60 70 80 90 100\n" ] } ], "source": [ "for row in range(1, 11):\n", " for col in range(1, 11):\n", " print(f'{row*col:4}', end ='')\n", " \n", " print()" ] }, { "cell_type": "code", "execution_count": null, "id": "e8c2f9fe-8dc0-46f2-80f0-efca34bdc612", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }