-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-casting.html
More file actions
347 lines (336 loc) · 15.1 KB
/
Copy pathselection-casting.html
File metadata and controls
347 lines (336 loc) · 15.1 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
<!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="selection.html" class="btn btn--nav btn--nav-left" title="Previous page (Selection)">‹</a>
<h2 class="page-title">Selection with casting </h2>
<a href="iterationWhile.html" class="btn btn--nav btn--nav-right" title="Next page (While loops)">›</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">
Quick Reminder
</button>
<button class="tab-button" data-tab="casting">
Casting to Integers
</button>
<button class="tab-button" data-tab="comparison">
Comparison Operators
</button>
<button class="tab-button" data-tab="combining">
Combining Concepts
</button>
<button class="tab-button" data-tab="example">
Complete Example
</button>
</div>
<div class="tab-content active" id="intro">
<h1>Selection with Casting</h1>
<p>
You've already learned how to use <span class="highlight">if-else statements</span> to make decisions based on string comparisons using <code>==</code>.
Now we're leveling up! Today you'll learn how to:
</p>
<ul>
<li><span class="highlight">Cast</span> user input to integers using <code>int()</code></li>
<li>Use <span class="highlight">comparison operators</span> like <code><</code>, <code>></code>, <code><=</code>, and <code>>=</code></li>
<li>Make decisions based on <span class="highlight">numerical comparisons</span></li>
</ul>
<h3>Quick Selection Reminder</h3>
<p>Remember the basic structure:</p>
<div class="code-example">
<code>
<div>if <em>condition</em>:</div>
<div><span class="tab"></span><em># Do this if condition is True</em></div>
<div>else:</div>
<div><span class="tab"></span><em># Do this if condition is False</em></div>
</code>
</div>
</div>
<div class="tab-content" id="casting">
<h2>Casting with int()</h2>
<p>
When you use <code>input()</code>, Python always gives you a <span class="highlight">string</span> (text), even if the user types a number!
To work with numbers mathematically, we need to <span class="highlight">cast</span> (convert) them to integers.
</p>
<div class="code-example">
<code>
<div># Without casting - this is a STRING</div>
<div>age = input("How old are you? ") <em># age is "18" (text)</em></div>
<div> </div>
<div># With casting - this is an INTEGER</div>
<div>age = int(input("How old are you? ")) <em># age is 18 (number)</em></div>
</code>
</div>
<p>
Why does this matter? Try this thought experiment:
</p>
<div class="code-example">
<code>
<div>ans = "18" + "1" <em># "181" because String concatenation</em></div>
<div>ans = 18 + 1 <em># 19 because addition</em></div>
</code>
</div>
<p>
By wrapping <code>input()</code> with <code>int()</code>, we tell Python:
"Take the user's input and cast it to an integer."
</p>
</div>
<div class="tab-content" id="comparison">
<h2>Comparison Operators</h2>
<p>
You've used <code>==</code> to check if two strings are exactly the same.
But with numbers, we can do so much more. Here are the <span class="highlight">comparison operators</span>:
</p>
<table class="summary-table">
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
<th>Example</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>==</code></td>
<td>Equal to</td>
<td><code>5 == 5</code></td>
<td>True</td>
</tr>
<tr>
<td><code><</code></td>
<td>Less than</td>
<td><code>3 < 5</code></td>
<td>True</td>
</tr>
<tr>
<td><code>></code></td>
<td>Greater than</td>
<td><code>7 > 5</code></td>
<td>True</td>
</tr>
<tr>
<td><code><=</code></td>
<td>Less than or equal to</td>
<td><code>5 <= 5</code></td>
<td>True</td>
</tr>
<tr>
<td><code>>=</code></td>
<td>Greater than or equal to</td>
<td><code>6 >= 5</code></td>
<td>True</td>
</tr>
<tr>
<td><code>!=</code></td>
<td>Not equal to</td>
<td><code>4 != 5</code></td>
<td>True</td>
</tr>
</tbody>
</table>
<p>
These operators let you ask questions like "Is this number bigger than that one?"
or "Is this value less than or equal to the limit?"
</p>
</div>
<div class="tab-content" id="combining">
<h2>Combining Casting and Comparison</h2>
<p>
Now let's put it all together! Here's how we can check if someone is old enough:
</p>
<div class="code-example">
<code>
<div>AGE_LIMIT = 18</div>
<div>age = int(input("How old are you? "))</div>
<div> </div>
<div>if age < AGE_LIMIT:</div>
<div><span class="tab"></span>print("You are too young")</div>
<div>else:</div>
<div><span class="tab"></span>print("You are old enough")</div>
</code>
</div>
<p>
Let's break down what happens:
</p>
<ol>
<li><code>AGE_LIMIT = 18</code> - We set the limit. Using a constant makes the code easier to understand.</li>
<li><code>age = int(input(...))</code> - We get user input AND cast it to an integer</li>
<li><code>if age < AGE_LIMIT:</code> - We compare the two numbers</li>
<li>The program decides which message to print based on the comparison</li>
</ol>
<p>
<strong>Try it mentally:</strong> What happens if the user enters <code>15</code>?
<span class="spoiler">15 < 18 is True, so it prints "You are too young"</span>
<br>
What about <code>21</code>?
<span class="spoiler">21 < 18 is False, so it goes to else and prints "You are old enough"</span>
</p>
</div>
<div class="tab-content" id="example">
<h2>Complete Example</h2>
<p>Here's a full program that checks if someone can watch a 15-rated film:</p>
<div class="code-example">
<code>
<div>AGE_LIMIT = 18</div>
<div>age = int(input("How old are you? "))</div>
<div> </div>
<div>if age < AGE_LIMIT:</div>
<div><span class="tab"></span><em># The condition is True when age is less than AGE_LIMIT</em></div>
<div><span class="tab"></span><em># e.g.: if age is 15, then 15 < 18 is True</em></div>
<div><span class="tab"></span>print("You are young")</div>
<div>else:</div>
<div><span class="tab"></span><em># The condition is False otherwise</em></div>
<div><span class="tab"></span><em># e.g.: if age is 20, then 20 < 18 is False</em></div>
<div><span class="tab"></span>print("You are old")</div>
</code>
</div>
<h3>Step-by-Step Walkthrough</h3>
<ol>
<li><strong>Line 1:</strong> Create a variable <code>age_limit</code> and assign the number 18 to it</li>
<li><strong>Line 2:</strong> Ask the user "How old are you?", take their answer (e.g., "15"), cast it to an integer (15), and assign it to <code>age</code></li>
<li><strong>Line 4:</strong> Check if <code>age < age_limit</code> (is 15 less than 18?)</li>
<li><strong>Line 7:</strong> If the condition is True, print "You are young"</li>
<li><strong>Line 8:</strong> If the condition is False, skip to else</li>
<li><strong>Line 11:</strong> Print "You are old"</li>
</ol>
<p>
<strong>Remember:</strong> Always use <code>int()</code> when you need to do numerical comparisons with user input!
</p>
<p>
Now it's your turn! Press <a href="#" class="inspire-link">inspire me</a> to practice with different scenarios involving numbers, limits, and comparisons.
</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-casting.js"></script>
</body>
</html>