forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.html
More file actions
612 lines (502 loc) · 27.7 KB
/
conditionals.html
File metadata and controls
612 lines (502 loc) · 27.7 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Introductory Programming in Python: Flow Control: Conditionals</title>
<link rel='stylesheet' type='text/css' href='style.css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src="animation.js" type="text/javascript">
</script>
</head>
<body onload="animate_loop()">
<div class="page">
<h1>Introductory Programming in Python: Lesson 6<br />
Flow Control: Conditionals</h1>
<div class="centered">
[<a href="program_state.html">Prev: Variables</a>] [<a href="index.html">Course Outline</a>] [<a href="while_loops.html">Next: Flow Control: Conditional Loops</a>]
</div>
<h2>Booleans</h2>
<p> Up to now, our programs have been pretty straightforward. The computer has simply executed each statement, in order, one after the other. There has been no variation, no change in behaviour, no awesomeness. In life, we are able to vary our behaviour based on our circumstances, ie we don't do <strong>exactly</strong> the same thing every day, day after day. Here are some everyday situations you might find yourself in, and some of the actions you might take as a result:</p>
<ul>
<li>If it's raining or snowing for that matter, you won't go out, not without an umbrella at least.</li>
<li>If its dark and your lights are off, you'll turn them on, provided you're not trying to sleep.</li>
<li>If you're hungry, and you have food at your disposal, or you have money to buy food, you'll eat something.</li>
</ul>
<p>We are capable of using our reasoning and intuition, when deciding what to do. Computers however don't have intuition, and the only reasoning they have is the reasoning <strong>you</strong> provide them with.
When analysing a situation we are capable of taking various complicated aspects into consideration. For example:</p>
<ul>
<li> How badly do I want to?</li>
<li> How many people we be there?</li>
<li> How wet will I get?</li>
<li> How much trouble will I get into?</li>
</ul>
<p> Computers on the other hand can not. Whilst we can weigh up different pros and cons and are capable of bending our own rules and even after hours of deliberating we're still capable of remaining unsure, a computer decides iether yes or no. There is no in between feelings, doubts or changing its mind.
The logic computers use to make their decisions is called boolean logic. For a computer, something can only have two states, <strong>True</strong> or <strong> False</strong>. A computer will ask these questions instead:</p>
<ul>
<li> Do I want to?</li>
<li> Will there be more than 5 people there?</li>
<li> Will I get wet?</li>
<li> Will I get into trouble?</li>
</ul>
<p> A computer can analyze certain conditions, determine if they are true or false, and then act accordingly. There is no room for leeway, no randomness, the computer is always sure that what its doing is correct. How does it know that it's correct? Because you told it it was.</p>
<p>In addition to computers only being aware of two conditions,
programming languages generally have very few ways of determining what
those conditions are and acting upon them. Lets have a look at that second example, "Will there be more than 5 people there":<p>
<pre class='listing'>
>>> numOfPeople=7
>>> numOfPeople>5
True
>>> numOfPeople=-3
>>> numOfPeople>5
False
>>> numOfPeople=5
>>> numOfPeople>5
False
>>>
</pre>
<p>In the above example, we set the numOfPeople first to 7, then to -3 then to 5. See how the expression <code>numOfPeople>5</code> only returns <code>True</code> when the value stored in the expression is <strong>more</strong> than 5, and returns <code>False</code> otherwise.
<p>'True' and 'False' are special words to python, known as
<strong>reserved words</strong>, meaning they have been reserved for a
particular purpose. Other reserved words we have encountered include
'print', and 'if'. The complete list of python's reserved words can be
found <a class="doclink"
href="http://olympiad.cs.uct.ac.za/docs/python-docs-2.6/reference/lexical_analysis.html#keywords">here</a>.</p>
<p>Note that when forming our proposition we used a specific operator,
the greater than sign ('>'). When forming propositions we can use a
specific set of operators, called <strong>comparison
operators</strong>. If both 'a' and 'b' are expressions of any
type:</p>
<ul>
<li><code>a == b</code> proposes Equivalence. Is 'a' equal to 'b'?
<strong>Note the double equals</strong>. In python '=' means
assignment, and '==' means is 'a' equal to 'b'?.</li>
<li><code>a != b</code> proposes Non-equivalence. Is 'a' not equal
to 'b'?</li>
<li><code>a < b</code> proposes Less Than. Is 'a' less than
'b'?</li>
<li><code>a > b</code> proposes Greater Than. Is 'a' greater
than 'b'?</li>
<li><code>a <= b</code> proposes Less Than OR Equal To. Is 'a'
less than or equal to 'b'?</li>
<li><code>a >= b</code> proposes Greater Than OR Equal To. Is
'a' greater than or equal to 'b'?</li>
<li><code>a in b</code> proposes that the <strong>string</strong> a
is a substring of, i.e. exists <strong>in</strong>side, the
<strong>string</strong> 'b'. This can also be used in lists as Marco will explain later.
</ul>
<p>Propositions, being expressions themselves, follow the usual rules
for composition of expressions. This means that we can have an
expression, <code>(x > 3) == (y < 4)</code>, in which we have two
propositions 'x > 3' and 'y < 4' which have been composed into
the total expression by using the third proposition which proposes that both sides have the same value. The entire proposition is true only if both the
sub-propositions have the same truth value at the time the expression
is evaluated.</p>
<p>So back to our program to decide if we want to go to the party, we don't want to go to lame parties with too few people. So we only go to the party if there are more than 5 people there, ie if <code>numOfPeople>5</code>. But what if we don't like crowded parties either? We could evaluate if <code>numOfPeople<40</code>, so as to avoid parties with more than 40 people. But if only there were a way to combine them, so that we only go to parties that have a couple people <strong>AND</strong> are not too crowded. Say hello to <strong>logical operators</strong>, also known
as <strong>boolean operators</strong> (operators which apply to boolean expressions). There
are only three logical operators, listed below. If 'expr1' and 'expr2'
are both propositions:</p>
<ul>
<li><code>expr1 and expr2</code> is True if both 'expr1'
<strong>and</strong> 'expr2' are True when the proposition is
evaluated.</li>
<li><code>expr1 or expr2</code> is True if either 'expr1'
<strong>or</strong> 'expr2' are True when the proposition is
evaluated.</li>
<li><code>not expr1</code> is True if 'expr1' is False, and <i>vice
versa</i>.</li>
</ul>
<p> You can use boolean operators to string together multiple statements. eg. to decide whether or not to go to a party you might use the following conditions</p>
<p><code>numOfPeople>5 <strong>and</strong> numOfPeople<40 <strong>and</strong> (day=='Friday' <strong>or</strong> day=='Saturday')</code></p>
<p> This will produce true if the party has between 5 and 40 people, and its iether a Friday or a Saturday. <strong>Note the brackets around the parts next to the or</strong>. Brackets are used here to define which expression to evaluate first. This is because the evaluation of boolean algebra also follows a hierachy system, </strong>BODMASCNAO.</strong> Your standard operations on numbers and variables come first. Then comes your conditions (>,==,<,in) then comes <strong>not</strong> then <strong>and</strong> and finally comes <strong>or</strong>. So if we left out the brackets and only had: </P
<p><code>numOfPeople>5 <strong>and</strong> numOfPeople<40 <strong>and</strong> day=='Friday' <strong>or</strong> day=='Saturday'</code></p>
<p> This could be interpreted as yes if the party has between 5 and 40 people and its a Friday, or its a Saturday (In which case the number of people doesn't matter). Hence its very important to get your BODMAS right else you're going to be going to lame parties with only 2 other people, just because its a Saturday. Lets look at some examples:</p>
<pre class='listing'>
>>> 3>4 and 25==20
False
>>> a=2==1 or 4+2>=6 and not 5%2==0
>>> a
True
>>> b=True and 1==1 and (3!=3 or 14-2>5 and 5==2*2)
>>> b
False
>>> 'ell' in "Hello There"
True
>>> b or a and 4<=3
False
>>> a or b or 2!=2
True
>>>
</pre>
<p><i>Note how in that example we stored the result of a boolean expression to a variable (variables a and b), and when we called them later they acted just like any other boolean expression.</i><br> So now we know how to determine from a set of conditions whether or not something is true. We know whether or not to go to the party, but that isn't the end of the story: we have to actually use that information to do something about it.</p>
<p>Python specifically has three methods to act upon such information, the most prevalent of which is the <a class="doclink"
href="http://olympiad.cs.uct.ac.za/docs/python-docs-2.6/reference/compound_stmts.html#if">if statement</a>. Put the
following in a file called 'second.py':</p>
<pre class='listing'>
x = raw_input("Enter a number: ")
if x > '3':
print "You entered", x
print "'x' is larger than 3"
</pre>
<p>Now run your program using <code>python second.py</code> and enter
'2' when prompted.</p>
<pre class='listing'>
sirlark@hephaestus ~/scratch $ python second.py
Enter a number: 2
</pre>
<p>Hmmmm! Why didn't our print statements execute? If, as we believe,
the program executes from top to bottom, the print statements should
have been executed. Obviously, that <code>if x > '3':</code> is
doing something we are unaware of, so let's experiment a little more.
Run the program again, but this time enter '5' at the prompt.</p>
<pre class='listing'>
sirlark@hephaestus ~/scratch $ python second.py
Enter a number: 5
You entered 5
'x' is larger than 3
</pre>
<p>Okay! Our program just acted differently based on the input we gave
it, or rather, based on the conditions determined by the input. Looking
back at the last three lines of our program, there's a lot of new stuff
in there that needs explanation, so we'll break it down. The if
statement takes the format:</p>
<pre class='listing'>
if <expression>:
statement
statement
...
</pre>
<p>What 'if' does, is check whether the expression provided has a value
of <em>True</em>, and if so, the statement immediately following the 'if' is
executed, otherwise it is not executed.
Taking our previously expressed English examples:</p>
<ul>
<li><em>If</em> there are between 5 and 40 people, and its iether a friday or a saturday, then <strong> go to the party</strong>
<li><em>If</em> you'll get wet, <strong>don't go outside</strong></li>
<li><em>If</em> its dark in your room, and the lights are off, <strong>turn them on</strong></li>
</ul>
<p>We can now express them in python as follows</p>
<pre class='listing'>
if numOfPeople>5 and numOfPeople<40 and (day=='Friday' or day=='Saturday'):
whatYoureDoing="going to the party"
</pre>
<pre class='listing'>
if weather=='Raining' and 'raincoat' not in thingsImWearing:
pass #In other words, don't go outside
</pre>
<pre class='listing'>
if dark and lightSwitch='off':
print "Go turn on your light"
</pre>
<p>Note the statement <code>pass</code>. Pass
is another reserved word in python, and means quite simply 'do
nothing'. We need it because 'if statements' <strong>require</strong> a
statement to execute if the expression they evaluate is True.</p>
<h3>Pitfalls in Translation</h3>
<p>We need to be aware of some common pitfalls that we will encounter
when translating English language logic (forgive the oxymoron) into
mathematical or computer logic. Examine the following examples:</p>
<ul>
<li>English: If both first name and surname are not blank<br />
<span class='red'>
Wrong: if firstname and surname != ''
</span><br />
<span class='green'>
Correct: if (firstname != '') and (surname != '')
</span></li>
<li>English: If the premises contain more than 3 persons and no
children or pets - <i>This is actually a very ambiguous statement</i><br>
<span class='red'>
Wrong: if adults > 3 and children or pets = 0
</span><br />
<span class='green'>
Correct: if persons > 3 and (children == 0 and pets == 0)
</span></li>
<li>English: If the type is not 5 or 6<br />
<span class='red'>
Wrong: if type != 5 or 6
</span><br />
<span class='green'>
Correct: if type != 5 and type != 6<br>
Correct: if not (type==5 or type==6)
</span></li>
</ul>
<p>Check out the <a class="doclink"
href="http://olympiad.cs.uct.ac.za/docs/python-docs-2.6/reference/expressions.html#Booleans">python documentation
page on boolean operators</a> for more detailed information.</p>
<h2>Executing Multiple Statements Conditionally</h2>
<div class="summary">
What 'if' does, is check whether the expression provided has a
value of <em>True</em>, and if so, the statement immediately
following is executed, otherwise it is not executed.
</div>
<p>Examining what we said previously, we notice nasty phrases like 'the
statement', indicating one, singular, statement. However, most of the
time we'll have a number of statements that we'll only want to execute when certain
conditions have been met. Python, like most programming languages,
provides us with the ability to group collections of statements into
<strong>blocks</strong>, much like composition of expressions. Such
blocks can be treated as a single statement for the purposes of if
statements, and others we will encounter soon.</p>
<p>Unlike most other languages python allows us to specify blocks using
<strong>indentation</strong> <i>(Spacing)</i>. Lines of code that have the same
indentation are considered to be members of the same block. Blocks can
also be <strong>nested</strong> inside one another, such lines
constituting inner blocks are also members of their encompassing outer
blocks. This is best illustrated using an example. (line numbers are
not part of the code)</p>
<pre class='listing'>
1: #this program takes three numbers as input, and counts how many of
2: #those numbers is even, and how many even numbers are negative
3: #if an even number is entered, if it is negative, a message is printed
4:
5: #set the counts for even and negative to 0
6: even = 0
7: negative = 0
8:
9: #get the first number
10: first = int(raw_input("Enter the first number: "))
11: if first % 2 == 0:
12: print "The first number is even"
13: even = even + 1
14: if first < 0:
15: print "The first number is negative"
16: negative = negative + 1
17:
18: #get the second number
19: second = int(raw_input("Enter the second number: "))
20: if second % 2 == 0:
21: print "The second number is even"
22: even = even + 1
23: if second < 0:
24: print "The second number is negative"
25: negative = negative + 1
26:
27: #get the third number
28: third = int(raw_input("Enter the third number: "))
29: if third % 2 == 0:
30: print "The third number is even"
31: even = even + 1
32: if third < 0:
33: print "The third number is negative"
34: negative = negative + 1
35:
36: print "There were", even, "even numbers and", negative, "of those were negative"
</pre>
<p>Running this program and giving the numbers 2, -4, and 7 as input
produces</p>
<pre class='listing'>
sirlark@hephaestus ~/scratch $ python illus.py
Enter the first number: 2
The first number is even
Enter the second number: -4
The second number is even
The second number is negative
Enter the third number: -7
There were 2 even numbers and 1 of those were negative
</pre>
<p>Firstly, let's get the indentation and nesting thing out of the way.
Looking at lines 12 through to 16 we see they are more indented than
the if statement on line 11. Thus these lines form a block of
statements. Since this block is immediately after an if statement, all
the statements in the block will only be executed if the if statement's
expression is True. For the first number we gave as input (2), this is
the case, so we start executing the block. When we get to line 14 we
encounter a second if statement. This second if statement is
<strong>nested</strong> inside the first, because it will only be
executed if the outer if statement (line 11) has an expression that
evaluates to True. Lines 15 and 16 form a <strong>nested
block</strong>, but are not executed because 2 is not negative, or more
specifically because 2 is not less than 0. Conversely, lines 24 and 25
will be executed because -4 is less than 0. Although -7 is negative, lines 33 and 34 won't be executed, because the if statement on line 32 is part of the block from 30-34 that never gets executed</p>
<p>Despite the fact that this example is an order of magnitude larger
than any previous examples, there are only two new things. The first
being indentation and nesting, the second being the use of
<code>int()</code> around our raw_input functions. Remember that
raw_input() returns a string. So even if the user types in 2,
raw_input() returns not 2 but the string '2'. Strings and numbers don't
really play nice together, especially when they're being compared to
one another, so we need to convert the string into an integer. This is
known as <strong>type casting</strong> or <strong>coercion</strong>. We
can do it for any basic python type ...</p>
<pre class='listing'>
>>> float("3.1415")
3.1415
>>> str(143)
'143'
>>> bool(None)
False
>>> bool(-1)
True
>>> int(1.4142)
1
>>>
</pre>
<h2>Nested if's vs complex boolean expressions</h2>
<p> Lets re-examine our party scenario. We could use nested <em>if</em>s to specify when to go to the party as such:</p>
<pre class='listing'>
if numOfPeople>5:
if numOfPeople<40:
if day=='Friday':
print "Go to party"
if day=='Saturday':
print "Go to party"
</pre>
<p> This method will work, but it is cumbersome. Too many nested if's can lead to confusion, and if you wanted to change your program later so that it printed "You should go to the party", you might change it in one place but not the other. As such it would be much better to write that algorithm as follows:</p>
<pre class='listing'>
if numOfPeople>5 and numOfPeople<40 and (day=='Friday' or day=='Saturday):
print "Go to the party"
</pre>
<p> This is a much more elegant solution, and is preferable. You would only use nesting when you want to do slightly different things depending on the situation. When programming, less is more. <strong>You should almost always strive for less code</strong> (so long as the program still does what it should).<p>
<h2>Specifying Execution Code for Alternate Conditions</h2>
<p>So now we have a handle on basic if statements, we come up against
the next hurdle. If our condition isn't met, our if block statements
simply aren't executed. But what if we want something
<strong>else</strong> executed instead of nothing when the condition
isn't met. We could simply follow our first <em>if</em> with another <em>if</em> that has the opposite expression (using not), but this isn't a very elegant solution. Enter the <em>if ... else</em> statement.</p>
<pre class='listing'>
1: first = int(raw_input("Enter a number: "))
2: if first % 2 == 0:
3: print first, "is even"
4: else:
5: print first, "is odd"
</pre>
<p>Now, if 'first' is not even, the program will print a message saying
it is odd, instead of doing nothing. The indentation is of key
importance here again. Note that 'else' is at the same indentation
level as the 'if' and is also followed by a colon (':'). The statement
to be executed if 'first' is not even, on line 5, is known as the
<strong>else clause</strong>, as the statement on line 3 is known as
the <strong>if clause</strong>.</p>
<p>Finally we have to deal with the case where things are not as clear
cut, not so black and white.</p>
<pre class='listing'>
1: colour = raw_input("Enter the name of a colour: ")
2: if colour == "Red":
3: print "Stop!"
4: elif colour == "Orange":
5: print "Try to stop if possible."
6: elif colour == "Green":
7: print "Go! Go! GO!"
8: else:
9: print colour, "Stop! You're obviously drunk."
</pre>
<p>Running this a couple of with various inputs we get</p>
<pre class='listing'>
sirlark@hephaestus ~/scratch $ python illus.py
Enter the name of a colour: Red
Stop!
sirlark@hephaestus ~/scratch $ python illus.py
Enter the name of a colour: Green
Go! Go! GO!
sirlark@hephaestus ~/scratch $ python illus.py
Enter the name of a colour: orange
Stop! You're obviously drunk.
sirlark@hephaestus ~/scratch $
</pre>
<p>The first run things happen as expected; We get to line 2, the value
of colour is <em>Red</em>, the expression for the if statement is True,
we execute line 3 ... and then the program finishes! This is because
line 4 is not part of the block constituting line 2's if clause,
because it has a lower indentation level. Also line's 4, 6 and 8 are
all still part of the same if statement. Let's look at the second run a
little more closely to see what happens.</p>
<p>We get to line 2, the value of colour is <em>Green</em>.
<em>Green</em> is not equal to <em>Red</em>, so we don't execute line
3. Instead we jump to the else ... hang on! What is 'elif'? 'elif' is
short for 'else if' and allows one to set up alternate conditions if
earlier conditions haven't been met. So we get to the 'elif' on line 4.
<em>Green</em> is not <em>Orange</em> so we don't execute line 5, instead we go onto line 6, which is still part of the same if-else group. <em>Green</em> is equal to <em>Green</em> so that will return true and we will run line 7.</p>
<p>The third run illustrates nicely that in fact strings in python are
case sensitive too. Not just the language, but the data we manipulate
with it is considered case sensitive. 'Orange' and 'orange' are different
strings, hence niether the if, nor any of the elifs execute. As a last resort, the block after the else executes.</p>
<h2>Formal Summary</h2>
<ul>
<li>Conditions are specified using propositions, i.e. expressions that have a value of either True or False.</li>
<li>Propositions are expressions and can be composed using other expressions combined with logical operators.</li>
<li>The 'if statement' is used to execute certain statements only if certain conditions are true when the 'if statement' is executed.</li>
<li>'If statements' can specify alternative conditions and the code to execute under those conditions using 'elif'.</li>
<li>'If statements' can specify what to execute if none of the conditions specified are true using 'else'.</li>
<li>A collection of statements can be grouped together in the same code block, by indentating them</li>
<li>The complete syntax of the if statement follows, where sections enclosed in square braces are optional.</li>
</ul>
<pre class='definition'>
if <expression>:
statement
statement
...
[elif <expression>:
statement
statement
...]
[elif <expression>:
statement
statement
...]
[...]
[else:
statement
statement
...]
</pre>
<h2>Exercises</h2>
<ol>
<li>Give a condition to check whether a number <em>x</em> is divisible by another number, <em>y</em>? <br><i>Hint: what does the operator <em>%</em> do?</i></li>
<li>Given the English phrase, "If you're not on the VIP list or
the staff list, you can't come in.", write down an equivalent python/psuedo code
condition.</li>
<li>Write a program that reads in a number and halves it, unless its an odd number, in which cass it must print a suitable error message.</li>
<li>Do you think the following is true or false?
<pre class='list'>'Two' < 'Three'</pre>
Evaluate it. Is the answer what you expected? How are strings compared to each other?
<li><pre class='listing'>
a=1
b=raw_input()
if a==b:
print "You inputted 1!"
</pre>
Write a program with the above code. Run it and give it 1 as an input. Does the program do as you expected? Why not? Try and fix it.
</li>
<!--<li>Think of a random person. Evaluate if the following returns true or false with regards to that person:
<pre class='list'>
(MyGender!=TheirGender and TheirAge>=MyAge/2+7 and TheirAge<=(MyAge-7)*2
and TheirAttractiveness+MyBloodAlcohol>=MyAttractiveness and
TheirAttractiveness-MyBloodAlcohol<=MyConfidence and (TheirSchoolMark<'C'
or TheirBreastOrMuscleSize>'C')
</pre></li>-->
<li>Write a program that asks the user to enter two numbers. If the
second number is not zero, print the quotient (the one number divided by the other) of the two numbers,
otherwise print a message to the effect of not being able to divide
by zero.</li>
<li>What does the following equate to? Try figure it out for yourself before you code it up. If you're still not sure why the answer is as it is, try breaking it up into smaller bits and evaluate them individually.
<pre class='list'>True and not True or (True or False and False) and False == (not True or False)</pre>
<li>Write a program that asks the user to enter 4 numbers, and
prints the smallest number entered.</li>
<li>Write a program that asks the user to enter 3 numbers, and
prints the largest even number entered, and displays a suitable error message if no even numbers are entered, or if there is no one largest number (ie. if the two biggest numbers are the same)</li>
<li>Write a program that reads in a sentence and a keyword and tells you if that keyword is in that sentence.</li>
<li>Write a program that asks the user to enter a four digit year,
and prints out whether that year is a leap year or not. Hint: Its a leap year if the year is divisible by four, except if its divisible by 100, in which case its not, except if its divisible by 400, in which case it is. ie. 1900 was not a leap year but 2000 was</li>
<li>Write a program that asks for the names, and the birthdays of two people. (Ask for year, month and then date of birth)The program must determine which of the two people is oldest.</li>
<li>Write a program that asks the user to enter 3 names, then
outputs them sorted alphabetically.</li>
<li>
<pre class='listing>
inport random
a=random.randrange(10)
print a
</pre> The above code prints a random number between 0 and 10. Use the above code in a program that asks the user to guess what number the computer is "thinking of". Then tell the user if he was right, or wrong, and if so, whether his guess was too high or too low.</i>
<li>Write a program that asks the user to enter a number, then
prints out all the numbers from 1 to 10 by which the entered number
is divisible.</li>
<li>Think about that last program you wrote and how much of the code was repeated. Write a better version of the same program in your own pseudo-code.
</ol>
<div class="centered">
[<a href="basic_input.html">Prev: Basic Input</a>] [<a href="index.html">Course Outline</a>] [<a href="while_loops.html">Next: Flow Control: Conditional Loops</a>]
</div>
</div>
<div class="pagefooter">
Copyright © James Dominy 2007-2008; Released under the <a href="http://www.gnu.org/copyleft/fdl.html">GNU Free Documentation License</a><br />
<a href="intropython.tar.gz">Download the tarball</a>
</div>
</body>
</html>