-
Notifications
You must be signed in to change notification settings - Fork 3
/
readme.html
93 lines (93 loc) · 3.23 KB
/
readme.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>readme</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="practice-exercises-for-organizing-with-functions">
Practice exercises for organizing with functions
</h1>
<h2 id="overview">Overview</h2>
<p>
In this section, you’ll have a chance to practice the concepts you’ve
learned in the videos. First, review the core concepts covered that you’ll
need to keep in mind. Then go through the exercises below.
</p>
<p>
Remember, these are for your own benefit. Feel free to skip them if you
don’t find a particular exercise valuable or you get stuck for too long.
</p>
<h2 id="core-concepts">Core concepts</h2>
<h3 id="function-with-parameters-input-data">
Function with parameters (input data)
</h3>
<p>
When we have code that we want to reuse or isolate and treat as a single
concept (black box), we define functions. Here is a basic function:
</p>
<pre><code>def say_hello(name):
print(f'Nice to meet you {name}')</code></pre>
<h3 id="function-that-generates-data-return-values">
Function that generates data (return values)
</h3>
<p>Functions can also return values to be used later:</p>
<pre><code>def get_name():
name = input("What is your name? ")
return name</code></pre>
<p>We can then use these functions together as follows:</p>
<pre><code>person = get_name()
say_hello(person)</code></pre>
<h3 id="a-main-method-and-running-the-program">
A main method and running “the program”
</h3>
<p>
As we saw, it’s a good convention to have an overall organizing function
that is what the whole program does at the top of the file. I called this
main, for example:
</p>
<pre><code>def main():
show_header()
get_names()
# ...</code></pre>
<p>
And you must remember to run this <strong>at the end</strong> of your
program to actually make it execute. We added these two lines as the final
of the source file:
</p>
<pre><code>if __name__ == "__main__":
main()</code></pre>
<h2 id="exercises">Exercises</h2>
<p>
Now it’s your turn. Your practice exercise is to
<strong
>take the M&M guessing game we created back in chapter 5
(interactive code) and clean it up using functions</strong
>. Make a copy of the file in this folder called
<code>guessinggame.py</code>. That’s what we started with. There are
probably 3-4 functions that you can create to help organize and isolate
parts of this application. Use the core concepts above to help.
</p>
</body>
</html>