-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8DogLookup.html
More file actions
58 lines (42 loc) · 4.31 KB
/
8DogLookup.html
File metadata and controls
58 lines (42 loc) · 4.31 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
<h3 id="problem-in-f17-2134-default-name">Problem in F17: 21.3.4) Default Name</h3>
<h3 id="problems-in-f18-394-default-name">Problems in F18: 39.4) Default Name</h3>
<h3 id="learning-objectives">Learning Objectives:</h3>
<ul>
<li>Create a simple dictionary</li>
<li>Use a function to get data from a dictionary</li>
<li>Use a global variable in a function</li>
</ul>
<h2 id="problem-below-is-a-dictionary-that-maps-the-names-of-dogs-to-their-breed-create-a-function-called-get_breed-that-takes-the-name-of-a-dog-and-returns-that-dogs-breed-if-the-dog-isnt-in-the-dictionary-return-no-dog-call-your-function-twice-to-test-it-and-print-the-result-once-with-a-name-in-the-dictionary-and-once-with-a-name-not-in-the-dictionary">Problem: Below is a dictionary that maps the names of dogs to their breed. Create a function called <code>get_breed</code> that takes the name of a dog and returns that dog’s breed. If the dog isn’t in the dictionary, return “No dog”. Call your function twice to test it and print the result: once with a name in the dictionary and once with a name not in the dictionary.</h2>
<pre class="codehilite"><code>dogs = {"Callie": "Pitbull", "Sasha": "Jack Russell", "Lexy": "Chihuahua", "Tiny": "Caucasian Shepherd", "Snowball": "Poodle"}</code></pre>
<h2 id="step-0-understanding-the-problem">Step 0: Understanding the problem:</h2>
<p>This problem gives us a dictionary to use as “global variable”. Here global means it goes at the top of our python program and is accessible from anywhere in our program, including in functions. Our task is to create a function that takes the name of a dog and tries to get the breed of the dog from the dictionary. If the name isn’t in the dictionary, the function should return “No dog”</p>
<h2 id="step-1-create-a-function-header">Step 1: Create a function header:</h2>
<p>Our function needs to be called <code>get_breed</code> and take a single parameter that holds a string representing the name of a dog. We’ll simply call this parameter name:</p>
<pre class="codehilite"><code>def get_breed(name):</code></pre>
<h2 id="step-2-check-for-a-key-in-the-dictionary">Step 2: Check for a key in the dictionary:</h2>
<p>The first thing we need to do is make sure <code>name</code> is in the dictionary. In python we can do this easily using the <code>in</code> keyword. When working with dictionaries, the expression <code>k in dictionary</code> will evaluate to true if the value stored in k is a key in dictionary. So to check if the dog called <code>name</code> is stored in the dictionary <code>dogs</code>, we write </p>
<pre class="codehilite"><code>name in dogs</code></pre>
<p>If this expression is true, we should get the dog’s breed from the dictionary. We’ll worry about that later so for now we’ll pass</p>
<pre class="codehilite"><code>if name in dogs:
pass</code></pre>
<p>Otherwise, we need to return “No dog”</p>
<pre class="codehilite"><code>if name in dogs:
pass
else:
return "No dog"</code></pre>
<h2 id="step-3-getting-a-value-from-the-dictionary">Step 3: Getting a value from the dictionary:</h2>
<p>Now that we’ve made sure the name is in our dictionary, we can get the breed from the dictionary using the name. To do this, we use square brackets with the key we want to search for. </p>
<pre class="codehilite"><code>return dogs[name]</code></pre>
<h2 id="step-4-testing-our-function">Step 4: Testing our function:</h2>
<p>We need to test both cases of our function: when name is in the dictionary and when it is not. This makes sure that both parts of our if statement work as they should. </p>
<pre class="codehilite"><code>print(get_breed("Tiny"))
print(get_breed("Eric"))</code></pre>
<h2 id="solution">Solution:</h2>
<pre class="codehilite"><code>dogs = {"Callie": "Pitbull", "Sasha": "Jack Russell", "Lexy": "Chihuahua", "Tiny": "Caucasian Shepherd", "Snowball": "Poodle"}
def get_breed(name):
if name in dogs:
return dogs[name]
else:
return "No dog"
print(get_breed("Tiny"))
print(get_breed("Eric"))</code></pre>