-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.html
More file actions
353 lines (338 loc) · 14.6 KB
/
Copy pathselection.html
File metadata and controls
353 lines (338 loc) · 14.6 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description" content="Learn selection and if/else statements with interactive Python programming practice questions and examples." />
<meta name="keywords" content="python programming, selection, if statements, else statements, conditional logic, practice questions, coding exercises, python tutorial" />
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
rel="stylesheet"
/>
<title>Programming Practice - Selection</title>
<!-- external CSS link -->
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div>
<header>
<h1>Programming practice</h1>
</header>
<main>
<div class="page-header">
<a href="variables.html" class="btn btn--nav btn--nav-left" title="Previous page (Variables)">‹</a>
<h2 class="page-title">Selection with if/else</h2>
<a href="selection-casting.html" class="btn btn--nav btn--nav-right" title="Next page (Selection with casting)">›</a>
</div>
<nav>
<a href="#" class="btn" id="background">Background</a>
<a href="#" class="btn" id="inspire">Inspire me!</a>
<a href="#" class="btn" id="help">Help me!</a>
</nav>
<section class="background hidden">
<div class="tab-container">
<div class="tab-buttons">
<button class="tab-button active" data-tab="intro">
Introduction
</button>
<button class="tab-button" data-tab="programming-constructs">
Programming Constructs
</button>
<button class="tab-button" data-tab="if-else">
If-Else Statements
</button>
<button class="tab-button" data-tab="equals-signs">
Equals Signs
</button>
<button class="tab-button" data-tab="indentation">
Indentation
</button>
<button class="tab-button" data-tab="example">
Putting It All Together
</button>
</div>
<div class="tab-content active" id="intro">
<h1>Selection in Python</h1>
<p>
Welcome to the world of decision-making in programming! Today,
we're going to learn about <span class="highlight">selection</span> , which is like teaching
your computer to make choices. It's a bit like being the
director of your own interactive movie!
</p>
</div>
<div class="tab-content" id="programming-constructs">
<h2>The Three Musketeers of Programming</h2>
<p>
In programming, we have three main ways to control how our code
runs. These are called <span class="highlight">programming constructs</span> :
</p>
<table class="summary-table">
<thead>
<tr>
<th>Construct</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td>sequence</td>
<td>
This is like following a recipe step by step and in order.
</td>
</tr>
<tr>
<td>selection</td>
<td>
This is about making choices, today's task, uses if, elif,
else.
</td>
</tr>
<tr>
<td>iteration</td>
<td>
This is about repeating things, but that's a story for
another day.
</td>
</tr>
</tbody>
</table>
<p>
Today, we're focusing on <span class="highlight">selection</span> , which is all about making
decisions in our code.
</p>
</div>
<div class="tab-content" id="if-else">
<h2>If-Else: Teaching Your Computer to Make Choices</h2>
<p>
In Python, we use '<span class="highlight">if</span> ' and '<span class="highlight">else</span> ' statements to make decisions.
It's like asking a question and doing different things based on
the answer.
</p>
<div class="code-example">
<code>
<div>if it_is_raining:</div>
<div><span class="tab"></span>print("Take an umbrella")</div>
<div>else:</div>
<div><span class="tab"></span>print("Enjoy the sunshine")</div>
</code>
</div>
<p>
In this example, <span class="highlight">if</span> it's raining, the program tells you to take
an umbrella. Otherwise (<span class="highlight">else</span> ), it tells you to enjoy the
sunshine.
</p>
</div>
<div class="tab-content" id="equals-signs">
<h2>The Tale of Two Equals: = vs ==</h2>
<p>In Python, we use <span class="highlight">equals signs</span> in two different ways:</p>
<table class="summary-table">
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>=</code></td>
<td>
This is for assigning values to variables. It's like
putting something in a box.
</td>
</tr>
<tr>
<td><code>==</code></td>
<td>
This is for comparing values. It's like asking "Are these
the same?"
</td>
</tr>
</tbody>
</table>
<div class="code-example">
<code>
<div>favourite_colour = "blue" # Assigning a value</div>
<div>if favourite_colour == "blue": # Comparing values</div>
<div><span class="tab"></span>print("Great choice!")</div>
</code>
</div>
</div>
<div class="tab-content" id="indentation">
<h2>The Importance of Indentation</h2>
<p>
In Python, <span class="highlight">indentation</span> (the space at the beginning of a line) is
super important! It tells Python which lines of code belong to
an if statement.
</p>
<p>After an if or else statement, you need to:</p>
<ol>
<li>Put a colon <span class="highlight">:</span> at the end of the line</li>
<li>
<span class="highlight">Indent</span> the next line (usually by pressing the Tab key <span class="highlight">⭾</span> once).
</li>
</ol>
<div class="code-example">
<code>
<div>if score > 10:</div>
<div><span class="tab"></span>print("You win!") # This line is
indented</div>
<div><span class="tab"></span>print("Well done!") # This line is
also indented</div>
<div>print("Game over") # This line is not indented, so it's not
part of the if</div>
</code>
</div>
<p>
What would happen if the score is more than 10? <span class="spoiler">It would output You win!, Well done! and then Game over.</span> How about if it is 10 or less? <span class="spoiler">It would just output Game over.</span>
Predict then mouse over to check your answer.
</p>
</div>
<div class="tab-content" id="example">
<h2>Putting It All Together</h2>
<p>Let's use everything we've learned to make a fun program:</p>
<div class="code-example">
<code>
<div>CORRECT_ANS = "spongebob squarepants"</div>
<div>best_show = input("What is the best TV show? ")</div>
<div>if best_show == CORRECT_ANS:</div>
<div><span class="background-comma">
<span class="tab"></span>print("Absolutely!", CORRECT_ANS,
"is glorious")</span></div>
<div><span class="background-plus">
<span class="tab"></span>print("Absolutely! " + CORRECT_ANS +
" is glorious")</span></div>
<div>else:</div>
<div><span class="background-comma">
<span class="tab"></span>print(best_show, "?!?! No way - it
is", CORRECT_ANS)</span></div>
<div><span class="background-plus">
<span class="tab"></span>print(best_show + "? No way - it
is " + CORRECT_ANS)</span></div>
</code>
</div>
<p>
This program:
<ol>
<li>Uses a constant to store the correct answer,</li>
<li>Asks the user to input a favourite TV show,</li>
<li>Checks the value input,</li>
<li>If it is "spongebob squarepants", it outputs a message agreeing with you,</li>
<li>Otherwise it outputs a different message saying that it is spongebob squarepants.</li>
</ol>
<span class="background-plus">We did not need to use <code>str()</code> to cast because the variables were already strings. </span>
</p>
<p>
Now it's your turn! Can you make a program that asks for a
password and only lets the user in if they enter the correct
one? Press <a href="#" class="inspire-link">inspire me</a> for more ideas of what to code.
</p>
</div>
</div>
</section>
<section class="example hidden">
<h3 class="question-header">
<button id="prevQuestion" class="btn btn--nav btn--nav-left" title="Previous question (← arrow key)">‹</button>
<span class="question-text">
Write a program to...
<span id="topic"></span>
</span>
<button id="nextQuestion" class="btn btn--nav btn--nav-right" title="Next question (→ arrow key)">›</button>
</h3>
<section class="cartoon1">
<img id="questionImage" src="img/baseImageSelection.webp" alt="" />
<p id="selectionQuestion"></p>
<p id="selectionAnswer1"></p>
<p id="ifReply"></p>
<p id="selectionAnswer2"></p>
<p id="elseReply"></p>
</section>
<section class="code hidden">
<button id="copyMe" class="btn btn--small btn--copy" pre="Click to copy code">
📋
</button>
<p id="codeCorrectAnswer"></p>
<p id="codeQuestion"></p>
<p id="codeIfCheck"></p>
<p id="codeIfReply"></p>
<p id="codeElse">else:</p>
<p id="codeElseReply"></p>
<p id="response"></p>
</section>
</section>
<div class="concatenation-toggle">
<label class="toggle-label">
<span class="toggle-text">Concatenation Mode:</span>
<div class="toggle-modes">
<span class="mode-comma">Comma (,)</span>
<input type="checkbox" id="concatToggle" class="toggle-checkbox">
<span class="toggle-slider"></span>
<span class="mode-plus">Plus (+)</span>
</div>
</label>
</div>
</main>
<button id="forceSpongebob" class="btn small-btn" pre="Spongebob">
🧽
</button>
<button id="keyboardShortcuts" class="btn small-btn" pre="Keyboard Shortcuts">
⌨️
</button>
<button id="themeToggle" class="btn small-btn" pre="Toggle Light/Dark Mode">
🌙
</button>
<!-- Keyboard Shortcuts Modal -->
<dialog id="shortcutsModal" class="shortcuts-modal">
<div class="modal-content">
<div class="modal-header">
<h2>Keyboard Shortcuts</h2>
<button class="modal-close" aria-label="Close modal">×</button>
</div>
<div class="modal-body">
<div class="shortcut-section">
<h3>Global Shortcuts</h3>
<div class="shortcut-item">
<kbd>i</kbd>
<span>Inspire me (generate random question)</span>
</div>
<div class="shortcut-item">
<kbd>b</kbd>
<span>Toggle background information</span>
</div>
<div class="shortcut-item">
<kbd>t</kbd>
<span>Toggle concatenation mode (comma/plus)</span>
</div>
<div class="shortcut-item">
<kbd>k</kbd>
<span>Show keyboard shortcuts</span>
</div>
</div>
<div class="shortcut-section">
<h3>When Question is Showing</h3>
<div class="shortcut-item">
<kbd>←</kbd>
<span>Previous question</span>
</div>
<div class="shortcut-item">
<kbd>→</kbd>
<span>Next question</span>
</div>
<div class="shortcut-item">
<kbd>h</kbd>
<span>Show/hide code help</span>
</div>
<div class="shortcut-item">
<kbd>c</kbd>
<span>Copy code to clipboard</span>
</div>
</div>
</div>
</div>
</dialog>
</div>
<script type="module" src="js/selection.js"></script>
</body>
</html>