-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2BePolite.html
More file actions
78 lines (55 loc) · 6.26 KB
/
2BePolite.html
File metadata and controls
78 lines (55 loc) · 6.26 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
<h3 id="trouble-problem-from-f17-1333-be-polite">Trouble problem from F17: 13.3.3) Be Polite</h3>
<h3 id="problem-in-s18-213-be-polite">Problem in S18: 21.3) Be Polite</h3>
<h2 id="learning-objectives">Learning Objectives:</h2>
<ul>
<li>Define/Call Function with return value</li>
<li>Define/Call a function with an argument</li>
<li>Define/Call Function with return value</li>
<li>Define/Call a function with an argument</li>
<li>Compare/contrast printing vs returning from a function </li>
<li>String Immutability</li>
</ul>
<h2 id="problem-you-want-to-be-able-to-go-to-a-website-by-simply-typing-the-name-of-the-website-instead-of-the-full-address-eg-tpying-google-instead-of-httpsgooglecom-create-a-function-called-make_address-that-takes-the-name-of-a-website-returns-the-website-name-with-htpps-added-to-the-beginning-and-com-added-to-the-end-call-this-function-with-your-2-favorite-websites-and-print-the-results-to-test-it">Problem: You want to be able to go to a website by simply typing the name of the website instead of the full address (e.g. tpying Google instead of https://google.com). Create a function called make_address that takes the name of a website returns the website name with “htpps://” added to the beginning and “.com” added to the end. Call this function with your 2 favorite websites and print the results to test it.</h2>
<h3 id="step-0-understanding-the-problem">Step 0: Understanding the problem:</h3>
<p>The goal here is to create a function that takes one string (the name of the website) and returns that string with something added to <br />
the beginning and the end. Then we have to test the function with a few of our favorite websites. </p>
<h3 id="step-1-create-the-function-make_address">Step 1: Create the function make_address:</h3>
<p>First thing we need to do is create a function that takes a single string. We do this using the <code>def</code> keyword (short for <strong>define</strong> function). A function definition has 4 parts: the <strong>def</strong> keyword, the name of the function, the names of the parameters the function takes, and a colon (‘:’). The <strong>def</strong> keyword goes first, followed by function name, then the parameter names in parenthesis, like below. </p>
<pre class="codehilite"><code>def function_name(parameter1, parameter2):</code></pre>
<p>In this case, the function name is make_address and it takes one parameter, we’ll call it “name”. So our function definition is:</p>
<pre class="codehilite"><code>def make_address(name):</code></pre>
<h3 id="step-2-creating-the-web-address">Step 2: Creating the web address.</h3>
<p>To combine two strings, we can use the <code>+</code> operator. So to build our website address we have to ‘add’ the string “https://” to the beginning of our parameter name and add ‘.com’ to the end.</p>
<pre class="codehilite"><code>"https://" + name + ".com"</code></pre>
<p>However, Strings in Python are <strong>immutable</strong> meaning they cannot be changed. So the expression above doesn’t change name, just like writing 5 + 2 doesn’t change the value of 5 or 2. So to make sure we keep the result we need to store it in a variable. </p>
<pre class="codehilite"><code>URL = "https://" + name + ".com"</code></pre>
<p>So now the variable URL holds the URL created using whatever website name was given to our function. </p>
<h3 id="step-3-return-the-url">Step 3: Return the URL</h3>
<p>Returning a value from a function is simply done using the <code>return</code> statement. Since the variable URL holds the value we want to return, we can write: </p>
<pre class="codehilite"><code>return URL</code></pre>
<p>to return the value stored in URL. Alternatively, we can skip the step of saving the value in a variable and put the string addition directly into the return statement. </p>
<pre class="codehilite"><code>return "https://" + name + ".com"</code></pre>
<h3 id="step-3-put-the-pieces-of-make_address-together">Step 3: Put the pieces of make_address together:</h3>
<p>Now that we have all the parts of our function, we can put it together to make a working function.</p>
<pre class="codehilite"><code>def make_address(name):
URL = "https://" + name + ".com"
return URL</code></pre>
<h3 id="step-4-testing-the-function">Step 4: Testing the function:</h3>
<p>Now that we’ve created the function <code>make_address</code>, we can call it a few times with different website names to test it. To call a function you write the function name with values you want to give parameters in parentheses. This can be done using a <strong>String literal</strong> (any characters between quotation marks that python reads as a value instead of code) or by putting variable names.</p>
<pre class="codehilite"><code>make_address("Google")</code></pre>
<p>gives our function the string literal “Google” </p>
<pre class="codehilite"><code>site_name = "Facebook"
make_address(site_name)</code></pre>
<p>gives our function the value stored in the variable site_name, which is “Facebook”<br />
We also need to make sure to print the return value of our function so we can see if the result is right. The return value can be stored in a variable which we can then print:</p>
<pre class="codehilite"><code>URL = make_address("Google")
print(URL)</code></pre>
<p>or we can put the function call inside print and print will use the return value directly</p>
<pre class="codehilite"><code>print(make_address("Facebook"))</code></pre>
<h3 id="solution">Solution:</h3>
<p>Now to put it all together, we need to make sure to put the function before we call the function. Since python reads the lines of your program, calling a function above the function itself means Python hasn’t seen that function yet, so as far as Python is concerned it doesn’t exist yet. We also have to make sure our indentation is correct so the calls to the function <code>make_address</code> is outside of the function. </p>
<pre class="codehilite"><code>def make_address(name):
URL = "https://" + name + ".com"
return URL
print(make_address("Facebook"))
print(make_address("Google"))</code></pre>