{ "cells": [ { "cell_type": "markdown", "id": "b62c6d0c-e1b1-4baa-9ab3-03daeb6b8ee5", "metadata": {}, "source": [ "
\n", "\n", "# Do it yourself #1\n", "\n", "Write a 'for' loop, which goes through the temperature array below, and prints out whether our temperature is above our below freezing. Have this loop print out the temperature, and in the same statement if it is above or below freezing. For example:\n", "\n", "`Temperature = 50, it is above freezing!`\n", "\n", "Use the temperature array provided below, or if you want, make up your own!" ] }, { "cell_type": "code", "execution_count": 1, "id": "434f2730-629e-4aef-a46c-e4a28ba95b3f", "metadata": {}, "outputs": [], "source": [ "temperature = [60,61,61,58,55,50,44,37,30,28,26,25,30,36,41,46,48]" ] }, { "cell_type": "code", "execution_count": 2, "id": "04fda0ca-07b6-4ff5-89d0-786bef6662b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "60\n", "Temperature = 60 it is above freezing\n", "61\n", "Temperature = 61 it is above freezing\n", "61\n", "Temperature = 61 it is above freezing\n", "58\n", "Temperature = 58 it is above freezing\n", "55\n", "Temperature = 55 it is above freezing\n", "50\n", "Temperature = 50 it is above freezing\n", "44\n", "Temperature = 44 it is above freezing\n", "37\n", "Temperature = 37 it is above freezing\n", "30\n", "Temperature = 30 it is below freezing\n", "28\n", "Temperature = 28 it is below freezing\n", "26\n", "Temperature = 26 it is below freezing\n", "25\n", "Temperature = 25 it is below freezing\n", "30\n", "Temperature = 30 it is below freezing\n", "36\n", "Temperature = 36 it is above freezing\n", "41\n", "Temperature = 41 it is above freezing\n", "46\n", "Temperature = 46 it is above freezing\n", "48\n", "Temperature = 48 it is above freezing\n" ] } ], "source": [ "for t in temperature:\n", " print(t)\n", " if t <= 32:\n", " print(\"Temperature = \",t,\"it is below freezing\")\n", " else:\n", " print(\"Temperature = \",t,\"it is above freezing\")" ] }, { "cell_type": "markdown", "id": "4c5f4839-f652-462b-89fc-7fffa6e84d7b", "metadata": {}, "source": [ "

\n", "\n", "# Do it yourself #2\n", "\n", "Write a for loop that loops through our temperature array that we listed below. \n", "For each iteration compute the difference between the temperature that you are iterating \n", "through in our list, with the previous temperature that we iterated through. For example,\n", "if we have a list of temperatures [30,50,45], and we are on the second iteration, we \n", "will substract 50 by 30 to get a temperature difference of 20. If the *absolute* temperature \n", "difference equals or exceeds 5 degrees, print out the result to the screen. In addition to\n", "printing our temperature difference values that exceed an absolute temperature difference of 5,\n", "students should also print out the time this occurs at, in the format of *HH:MM*. " ] }, { "cell_type": "code", "execution_count": 3, "id": "901d8797-2b3d-4622-9291-716cc81db8c6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24\n" ] } ], "source": [ "temperature = [42,37,35,34,34,35,36,38,41,44,47,52,58,65,66,67,67,64,61,56,52,47,41,39]\n", "hour = len(temperature)\n", "print(hour)" ] }, { "cell_type": "code", "execution_count": 6, "id": "cfb03b2a-cc30-4cc9-b078-a45eeb15ad8f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 12:00 6\n", " 13:00 7\n", " 22:00 6\n" ] } ], "source": [ "for h in range(1,hour):\n", " diff_abs = abs(temperature[h]-temperature[h-1])\n", " if(diff_abs>5):\n", " print(\" %2d:00 %d\" % (h,diff_abs))\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "40b128fd-03cc-41eb-9284-1c9d5e8f3304", "metadata": {}, "outputs": [], "source": [ "# better way \n", "abs_diff = temperature\n", "for t,temp in enumerate(temperature):\n", " if abs(temp) > 5:\n", " continue\n", " print(\" %d :00 %d}\" % (t,temp))" ] }, { "cell_type": "code", "execution_count": null, "id": "b87366fd-290f-4434-953d-b66896d7e8be", "metadata": {}, "outputs": [], "source": [ "for t,temp in enumerate(temperature):\n", " if abs(temp) > 5:\n", " continue\n", " print(\" %d :00 %d}\" % (t,temp))\n", " " ] } ], "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.10.5" } }, "nbformat": 4, "nbformat_minor": 5 }