-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7ConvertToFeels.html
More file actions
119 lines (89 loc) · 8.12 KB
/
7ConvertToFeels.html
File metadata and controls
119 lines (89 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<h2 id="problem-create-a-function-convert_to_feels-that-consumes-a-number-representing-a-temperature-and-returns-hot-if-it-is-above-70-cold-if-is-below-50-and-comfortable-otherwise">Problem: Create a function <code>convert_to_feels</code> that consumes a number (representing a temperature) and returns <code>"hot"</code> if it is above 70, <code>"cold"</code> if is below 50, and <code>"comfortable"</code> otherwise.</h2>
<p>Then, create a function <code>map_feels</code> that consumes a list of numbers (representing temperatures) and returns the list with each temperature converted to strings <code>"hot"</code>, <code>"cold"</code>, and <code>"comfortable"</code> (by using <code>convert_to_feels</code>).</p>
<p>Test convert_to_feels by calling it with a number and printing the result. Test map_feels by calling it with a list of numbers and printing the result.</p>
<h2 id="step-0-understanding-the-problem">Step 0: Understanding the problem:</h2>
<p>This question may seem like a lot to take in, but if we break it down into its parts we see it’s not too bad. </p>
<p>First it asks us to write the function <code>convert_to_feels</code>. All this function has to do is take in a number that represents a temperature and return a string describing the temperature (either hot, cold, or comfortable). This function can be done using an if statement on the number given. Then we have to test this function by calling it and printing the result. </p>
<p>Then it asks us to write the function <code>map_feels</code> that takes a list of numbers and returns a new list of the strings that represent the numbers in the given list. This is made easier with <code>convert_to_feels</code> because we can just call <code>convert_to_feels</code> on each number to get the string representation. Since we need to perform an operation on a list of values, we should use a for loop. We have to test this function by calling it with a list of numbers and printing the result.</p>
<h2 id="step-1-creating-the-function-header">Step 1: Creating the function header</h2>
<p>Based on our practice from previous examples, we can create the header for the function <code>convert_to_feels</code> that takes a single number:</p>
<pre class="codehilite"><code>def convert_to_feels(temp):</code></pre>
<p>And the function <code>map_feels</code> that takes a list of numbers:</p>
<pre class="codehilite"><code>def map_feels(temp_list):</code></pre>
<h2 id="step-2-writing-convert_to_feels">Step 2: Writing <code>convert_to_feels</code>:</h2>
<h2 id="step-2a-creating-an-if-statement-for-convert_to_feels">Step 2a: Creating an if statement (for <code>convert_to_feels</code>):</h2>
<p>Our function <code>convert_to_feels</code> is meant to capture the following logic:</p>
<pre class="codehilite"><code>If temp is above 70,
return hot
If temp is below 50,
return cold
Otherwise
return comfortable</code></pre>
<p>We can implement this logic in our function by simply using an if-elif structure:</p>
<pre class="codehilite"><code>if temp > 70:
return "hot"
elif temp < 50:
return "cold"
else:
return "comfortable"</code></pre>
<h2 id="step-2b-creating-the-function">Step 2b: Creating the function:</h2>
<p>Now that we have an if statement that does what our <code>convert_to_feels</code> function is meant to do, we can put it into our function.</p>
<pre class="codehilite"><code>def convert_to_feels(temp):
if temp > 70:
return "hot"
elif temp < 50:
return "cold"
else:
return "comfortable"</code></pre>
<h2 id="step-3-writing-map_feels">Step 3: Writing <code>map_feels</code>:</h2>
<h2 id="step-3a-creating-an-empty-list">Step 3a: Creating an empty list</h2>
<p>Since we need to build a list of strings that correspond to each number in <code>temp_list</code>, we need to create a new, empty list to hold the values as we generate them:</p>
<pre class="codehilite"><code>feels_likes = []</code></pre>
<h2 id="step-3b-creating-the-for-loop">Step 3b: Creating the for loop:</h2>
<p>We need to go through each entry in the list we are given and convert the temperature number to a feels string. We can do this with a simple for-each loop, like this: </p>
<pre class="codehilite"><code>for temp in temp_list:
# Do something</code></pre>
<p>The meaning of this for-each loop is relatively straightforward, it means “for each temp in temp_list, do something”, now we just have to figure out what “something” is. </p>
<h2 id="step-3c-use-function-to-get-a-value-and-add-to-a-list">Step 3c: Use function to get a value and add to a list:</h2>
<p>For every number in <code>temp_list</code>, we need to get the feels like string and add it to our <code>feels_likes</code> list. To get the feels like string, we can use our <code>convert_to_feels</code> function, passing it the number we are currently working with. In a for-each loop, the thing before the <code>in</code> keyword is called the item variable. In our case the item variable is <code>temp</code>. This variable is created just for the loop and is given each of the values in our list one at a time. Each time the item variable gets a new value, the “Do something” part of the loop, called the is called. So if there is something we want to do to every value in the list, we can put it inside the loop and assume the variable <code>temp</code> holds some value in our list. </p>
<p>For this problem, we need to use the <code>convert_feels</code> function to get the feels like string of temp, then add the string to <code>feels_likes</code>. To add to the end of the <code>feels_like</code> list, we simply use the <code>append()</code> function on the list:</p>
<pre class="codehilite"><code>feels_likes.append(some_value)</code></pre>
<p>But instead of some_value, we need to add the result of calling <code>convert_to_feels</code> on temp:</p>
<pre class="codehilite"><code>feels_likes.append(convert_to_feels(temp))</code></pre>
<h2 id="step-3d-putting-the-loop-together">Step 3d: Putting the loop together:</h2>
<p>Now that we have the parts of our loop, we can put it together:</p>
<pre class="codehilite"><code>for temp in temp_list:
feels_likes.append(convert_to_feels(temp))</code></pre>
<h2 id="step-3e-putting-the-function-together">Step 3e: Putting the function together:</h2>
<p>Now we can put everything we built in previous steps into our function: </p>
<pre class="codehilite"><code>def map_feels(temp_list):
feels_likes = []
for temp in temp_list:
feels_likes.append(convert_to_feels(temp))</code></pre>
<p>All that we have left to do is return the feels_likes list once the loop is finished. Make sure to put the return statement on the outside of the loop. Once a function reaches a return statement it is finished, so if we put the return statement on the inside of the loop our loop would run once, hit the return statement, then return and be finished. </p>
<pre class="codehilite"><code>def map_feels(temp_list):
feels_likes = []
for temp in temp_list:
feels_likes.append(convert_to_feels(temp))
return feels_likes</code></pre>
<h2 id="step-4-testing-our-functions">Step 4: Testing our functions:</h2>
<p>Now that both functions are written, we need to make sure they work properly.</p>
<p>First we should test the <code>convert_to_feels</code> function since <code>map_feels</code> uses it, so if <code>convert_to_feels</code> doesn’t work, neither will <code>map_feels</code>. </p>
<pre class="codehilite"><code>print(convert_to_feels(45))</code></pre>
<p>Now that we know <code>convert_to_feels</code> works, we can test <code>map_feels</code></p>
<pre class="codehilite"><code>print(map_feels([50, 90, 30])</code></pre>
<h2 id="solution">Solution:</h2>
<pre class="codehilite"><code>def convert_to_feels(temp):
if temp > 70:
return "hot"
elif temp < 50:
return "cold"
else:
return "comfortable"
def map_feels(temp_list):
feels_likes = []
for temp in temp_list:
feels_likes.append(convert_to_feels(temp))
return feels_likes
print(convert_to_feels(45))
print(map_feels([50, 90, 30]))</code></pre>