-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson6.html
211 lines (178 loc) · 8.68 KB
/
lesson6.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title> Lesson 6 | WMCode </title>
<link rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="wavefavicon.png" type="image/x-icon">
</head>
<body>
<div class="navbar">
<div class="navbarimg">
<a href="index.html" style="float: left; padding: 0; border: 0px solid black; background: none;">
<img src="WMCodeLogo.png" alt="Wave-Image">
</a>
</div>
<a href="resources.html">Resources</a>
<!-- <a href="details.html">Competition Details</a> -->
<!-- <a href="#calendar">Calendar</a> -->
<a href="https://repl.it/" target="_blank" rel="noopener noreferrer">REPL</a>
<!-- <a href="https://drive.google.com/drive/folders/1l8EKATqJjpaJWB3Y5UlMwQqiWEPop5sF?usp=sharing" target="_blank" rel="noopener noreferrer">Google Drive</a> -->
</div>
<div class="pagebody">
<h1> Lesson 6 </h1>
<h3> <a href="lesson6.zip" download> Download Lesson 6 Folder </a> </h3>
<hr>
<h2>Topics Covered:</h2>
<ul>
<li>Control Flow - Boolean Values</li>
<li>Worked Example of Boolean Applications</li>
</ul>
<h2>Lesson 6 Slides:</h2>
<iframe src="https://docs.google.com/presentation/d/e/2PACX-1vQyrNJIQSFlBhPE513yeLkuhEAXhUvtfrIBo32Lg9wfTJc88Lk9klKNbL8JXwur8XQV7BkS1Lzrd7Cp/embed?start=false&loop=false&delayms=3000"
frameborder="0" width="845" height="505" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
<!-- <h2>Lesson 6 Recording:</h2>
<img src="wavefavicon.png" height="400"></img> -->
</div>
<div class="pagebody">
<h2>Python Terminal:</h2>
Use this terminal to test the code below, and to experiment with the things in this lesson!
<b>IT MAY TAKE A MOMENT TO POP UP AND BE READY FOR USE</b>
<br>
To open the Python terminal:
<ul>
<li>Click the 'Shell' tab below</li>
<li>Type 'python3' (without the quotes) into the first line</li>
<li>Run code!</li>
</ul>
<b>NOTE:</b> Code will not auto-indent in the Python terminal, so you will have to use "tab" to indent
lines that need indentation.
<br>
<b>NOTE:</b> If the terminal does not let you keep typing in the, refresh the page and try again.
<br><br><br>
<iframe height="600px" width="80%" src="https://repl.it/@JesusEnriqueEnr/PiE-Terminal?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
</div>
<div class="pagebody">
<h2>Lesson Content:</h2>
<h3>Boolean Values - Motivation</h3>
To motivate what Boolean value is, and what it does, let's play a guessing game.
<br>
** See the slides for a full demo of the guessing game **
<h3>Coding Demo</h3>
To gain a better understanding of what is going one, it is best that we write what Python is actually interpreting
under its magical hood.
<br>
First, we intuitively write out our code with helper functions that will determine what is happening in
comparisons, without any effort. We compare their return values to <b>True</b> and <b>False</b> because it
makes sense. Here's what we will have at this point:
<pre>
real_num = 10
def over_under(guess):
“““over_under is a guessing game that uses user input to generate hints.”””
if guess == real_num:
print(“Yay! You guessed it!”)
elif is_over(guess) == True:
print(“Try something lower than that.”)
elif is_under(guess) == True:
print(“Try something bigger.”)
</pre>
After deciding that this implementation WILL work for anything, then we can move onto out helper functions, and
decide how to write them. **See the video for more about these helpers.** After writing them, they may look like:
<pre>
def is_over(guess):
return guess > real_num
def is_under(guess):
return guess < real_num
</pre>
<h3>What Just Happened?</h3>
<h4>As humans, we ask ourselves:</h4>
<i>“Is the current guess over or under the true value?”</i>
<br>
We get, as a response:
<i>“Yes”</i> or <i>“No”</i>.
<h4>Python is a little more sophisticated. It asks:</h4>
<i style="color: blue;">is_over(guess)</i> or <i style="color: blue;">is_under(guess)</i>?
<br>
It expects:
<i style="color: orange;">True</i> or <i style="color: orange;">False</i>.
<h3>Comparing Humans and Python</h3>
Both humans, and Python seek either of a:
<ul>
<li>Positive response, or</li>
<li>Negative response,</li>
</ul>
And will do something differently depending on the response.
For us, it is very easy to determine what is over/under, left/right, next/last, etc.
<br>
But, we have various different ways of expressing these things; a funny look, a slap on the wrist, getting yelled at, etc.
<br>
For Python, there are only certain things that can represent either a yes or no. These values are split into two
categories:
<ul>
<li>False-y values, and</li>
<li>Truth-y values</li>
</ul>
<h3>Python Boolean Values</h3>
<h4>False-y Values:</h4>
Python has 4 types of <b>False</b> values:
<ul>
<li>Empty objects</li>
<ul>
<li>Strings, Array, Lists, and Dictionaries</li>
<li>We will talk more about all of these in later lessons.</li>
</ul>
<li>The number 0</li>
<li>The value <i style="color: orange;">null</i>, and</li>
<li>The value <i style="color: orange;">False</i></li>
</ul>
<h4>Truth-y Values</h4>
Python has one main type of <b>True</b> value.
<ul>
<li>Everything that is not <b>False</b></li>
<ul>
<li>Objects with data stored in them.</li>
<li>Any number not 0, and</li>
<li>The value <i style="color: orange;">True</i></li>
</ul>
</ul>
<h3>Refactoring Our Code</h3>
Though our functions from earlier were nice, and pretty, they were actually quite clunky. By taking advantage of
boolean identities, we can actually make things a lot easier to read, and completely get rid of our helper functions.
<br>
Our new function may look something like this:
<pre>
real_num = 10
def over_under(guess):
“““over_under is a guessing game that uses user input to generate hints.”””
if guess == real_num:
print(“Yay! You guessed it!”)
elif guess > real_num:
print(“Try something lower than that.”)
elif guess < real_num:
print(“Try something bigger.”)
</pre>
(See the video for more notes on how this is actually better!)
<h3>Now, Why Are Booleans Useful?</h3>
Boolean values serve as additional levels of robustness for our code.
<br>
Booleans make special cases easy to catch, with minimal code.
<br>
Keeps redundancy away.
<br>
We don’t have to write functions to explicitly tell Python what is good or bad.
<h3>Final Note</h3>
If you are ever curious about what something in Python's boolean return value is, you can use the
built-in Python <b style="color: blue;">bool()</b> function with your item as the parameter, and Python
will tell you!!
<h1>...</h1>
</div>
<div class="footer">
<div class="footerbody">
<a>2021 | WMCode™</a>
<img src="wavefavicon.png" alt="WMCode-Logo">
<a href="mailto:[email protected]" style="float: right;">Contact Jesus</a>
</div>
<!-- <h1> ... </h1> -->
</div>
</body>
</html>