-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionals.py
More file actions
591 lines (415 loc) · 19.4 KB
/
Conditionals.py
File metadata and controls
591 lines (415 loc) · 19.4 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
'''Conditionals and Logical Operators: (<) (>) (<=) (>=) (==) (!=)'''
'''
Conditionals change the outcome of a program's execution run, depending \
on what the program is doing at the time. The conditionals in Python are the \
'if:' statement, 'elif:' statement and the 'else:' statement, along with the 'True:' \
and 'False:' statements. Conditionals are mainly used in conjunction with 'input' \
statements and conditional while-loops. However, Logical operators are also used \
to test whether a condition is less than (<), greater than (>), less than (<=) or equal \
to, greater than (>=) or equal to, equals (==) equals and not (!=) equal to. For example, 5 \
is greater (>) than 4 and 4 is less (<) than 5. Here are a few examples of logical operators, \
which test integer values against other integer values within 'print' statements. \
These 'print' statement illustration examples below will either display on the screen \
output as "True" or "False", depending on the outcome of the results.
print(4<5) True: 4 is less than 5
print(4>5) False: 4 is not greater than 5
print(4<=5) True: 4 is less than or equal to 5
print(4>=5) False: 4 is not greater than or equal to 5
print(4==5) False: 4 does not equal 5
print(4!=5) True: 4 does not equal 5
'''
'''-----------------------------------------------------------------------------'''
# Type and execute/run this simple 'print' statement program example below, using
# the logical operators in different combinations as was illustrated above and see
# what happens when you change the logical operators.
print(4<5)
# Screen output: True
'''-----------------------------------------------------------------------------'''
# Type and execute/run these 'print' statement program examples, using the (f')
# format implementer.
# The 'int' statement is for integer values only.
num=int(input('Type in a number and I will condition the result against 5 as either \
"true" or false" ').strip())
print(f'{num<5}')
print(f'{num>5}')
print(f'{num<=5}')
print(f'{num>=5}')
print(f'{num==5}')
print(f'{num!=5}')
'''Boolean Logic:'''
"IF" "ELIF" "ELSE" "TRUE" "FALSE" "AND" "OR" "NOT"
'''
There once was a man, named 'George Boole' who was a famous mathematician. He invented \
these conditionals called Boolean Logic, which indirectly brought about the computer age we \
now live. The conditionals of George Boole are as follows.
These conditionals are: 'IF', 'ELSE', 'TRUE', 'FALSE', 'AND', 'OR' 'NOT'
In computer terminology, these conditionals are called "Boolean Logic". Boolean Logic is simply \
all about the concept of decision making laws, meaning if something is true, then it is not false.\
Likewise, if something is false, then it is not true.
When it comes to program development, sometimes logical operators aren't enough alone to do \
the job. In most cases, other conditionals are needed to help the logical operators do the job. With \
the 'if:', 'elif:', 'else', 'true', 'false', 'and', 'or' and 'not' conditionals, the logical operators \
can do the job as they were designed for doing, which are to test values against other values and comparing \
data against user input data.
'''
'''-----------------------------------------------------------------------------'''
# Using simple 'print' statements, you can do simple True and False tests to help you
# determine the outcome of a conditional against another conditional, such as True
# and False conditionals. For example:
print(True and True)
print(False and False)
print(True and False)
print(False and True)
print(True or True)
print(False or False)
print(True or False)
print(False or True)
print(True and not True)
print(False and not False)
print(True and not False)
print(False and not True)
print(True or not True)
print(False or not False)
print(True or not False)
print(False or not True)
print(True is not True)
print(False is not False)
print(True is not False)
print(False is not True)
'''-----------------------------------------------------------------------------'''
# Use operators to check to see if a value is True or False.
print(True == True)
print(False == False)
print(True != False)
print(False != True)
print(True >= False)
print(True <= False)
'''-----------------------------------------------------------------------------'''
# Here is a prime example of how these conditionals work in conjunction with the
# logical operators. In this program example, the conditionals 'if:' and 'elif:' are
# implement along with the logical operators. The user is asked to type in a number, if
# the number is equal equals: == 5, the first conditional 'if:' statement is executed
# "print(f'True! {num} equals equals 5.')". If the number is less than 5, the first 'elif:'
# statement is executed "print(f'True! {num} is less than 5.')". If the number is greater
# than 5, the second 'elif:' statement is executed "print(f'False! {num} is not greater
# than 5.')". If the number is less than or equal to 5, the third 'elif:' statement is
# executed "print(f'True! {num} is less than or equal to 5.')". If the number is greater
# than or equal to 5, the last 'elif:' statement is executed "print(f'False! {num} is is not
# greater than or equal to 5.')".
# Note: Python executes/runs programs starting from the top, downward. Be very
# careful on how you place statements. Some statements cannot execute right, even if
# they work. This is simply because of the order that Python executes/runs its
# program statements in the backqround.
# Type and execute/run this program example below and see what happens.
# The 'int' statement is for integer values only.
num=int(input('Type in a number and I will condition the result against 5 as either \
"true" or false" ').strip())
if num==5:
print(f'True! {num} equals equals 5.')
elif num<5:
print(f'True! {num} is less than 5.')
elif num>5:
print(f'False! {num} is not greater than 5.')
elif num<=5:
print(f'True! {num} is less than or equal to 5.')
elif num>=5:
print(f'False! {num} is is not greater than or equal to 5.')
'''-----------------------------------------------------------------------------'''
# In this program example, the conditional 'else:' statement is executed only when the
# value 5 equals itself. Type and execute/run the program below and see what
# happens.
# The 'int' statement is for integer values only.
integer=int(input("Please enter an integer less than 5 or greater than 5: ").strip())
if integer<5:
print(f'{integer} is less then 5')
elif integer>5:
print(f'{integer} is greater than 5')
else:
if integer==5:
print(f'True! {integer} equals equals 5.')
'''-----------------------------------------------------------------------------'''
# Type and execute/run this program example and change the value 'num=5' to
# different values, such as 'num=9', 'num=-7'.....
num=6
if num<5:
print(f'{num} is less than 5')
elif num>5:
print(f'{num} is greater than 5')
else:
if num==5:
print(f'{num} equals equals 5.')
'''-----------------------------------------------------------------------------'''
# The conditionals 'True' and 'False' will only be true if both conditonals are true. They
# can also be true if both conditionals are false. Conditionals cannot be true and false
# at the same time, nor can it be 'yes' and 'no' at the same time. For example, if 'True'
# and 'True' are the same, they equal true. Likewise if 'False' and 'False' are the same,
# they too equal true. However 'True' and 'False' are not the same, so they equal false.
# Likewise False' and 'True' are not the same, so they equal false as well. Type and
# execute/run these rogram examples below.
conditional=False
if conditional==True:
print('True!')
elif conditional==False:
print('False!')
conditional=True
if conditional==False:
print('Both conditonals are true!')
print('True and True equals true.')
else:
print('Both conditonals are false!')
print('True and False equals False.')
conditional=False
if conditional==True:
print('Both conditonals are true!')
print('True and True equals true.')
else:
print('Both conditonals are false!')
print('False and True equals False.')
conditional=True
if conditional==True:
print('Both conditonals are true!')
print('True and True equals true.')
else:
print('Both conditonals are false!')
print('True and False equals False.')
conditional=False
if conditional==False:
print('Both conditonals are true!')
print('False and False equals true.')
else:
print('Both conditonals are false!')
print('True and False equals False.')
conditional=True
if conditional==True:
print('True!')
elif conditional==False:
print('False!')
'''-----------------------------------------------------------------------------'''
# This small program example waits for the user to type "True" or "False". If the user
# types 'true', then the 'print' statement 'print('True!')' is executed. If the user types
# 'false', then the 'print' statement 'print('False!')' is executed.
conditional=input('Type the words "True" or "False" ').strip()
if conditional=='true':
print('True!')
elif conditional=='false':
print('False!')
else:
print('Oops! Wrong keys:')
'''-----------------------------------------------------------------------------'''
# This program example waits for the user to type in a number against 5 to see if it's
# true or false. Type and execute/run this program example and type numbers, either
# less than 5 or greater than 5 or equal to 5.
try:
num=int(input('Type in a number and I will condition the result against 5 as either \
"true" or false" ').strip())
if num==5:
print(f'True! {num} equals equals 5.')
elif num<5:
print(f'True! {num} is less than 5.')
elif num>5:
print(f'False! {num} is not greater than 5.')
elif num<=5:
print(f'True! {num} is less than or equal to 5.')
elif num>=5:
print(f'False! {num} is is not greater than or equal to 5.')
except ValueError:
print('That is incorrect!')
'''-----------------------------------------------------------------------------'''
# I must give stern credit to Mosh from YouTube for this little conditional Python programming
# lesson. This one is quite mindful, even for me. All I did was simply change the
# words from starting and stopping the car, to staring and stopping a spacecraft.
# Search for Programming with Mosh on YouTube for more details.
# Type and execute/run this fun true/false program example below and see what
# happens when you type either 'START', 'STOP' 'HELP' or 'Q'.
start=False
while True:
command=input('SPACECRAFT\n').lower().strip()
if command=='start':
if start:
print('Spacecraft has already took off.')
else:
start=True
print('Spacecraft is taking off!')
elif command=='stop':
if not start:
print('Spacecraft has already landed.')
else:
start=False
print('Spacecraft is landing.')
elif command=='help':
print('Type "START" to fly the spacecraft or type "STOP" to land the spacecraft. \
Press "Q" to quit.\n')
elif command=='q':
break
else:
print(f'Sorry! cannot understand "{command}".')
'''-----------------------------------------------------------------------------'''
"AND" "OR" and "NOT"
'''
The 'and' statement is only "True", if both vales are true. If both values are "False", the 'and' \
statement is still true because they are the same, regardless of their values. However, if the 'and' \
statement values are different, nothing will happen, and this is where the 'or' statement comes into \
play. The 'or' statement will only be "True" if both values are different.
'''
# Here is another prime example of how these conditionals: 'True' and 'False' actually
# work in conjunction with 'and' and 'or'. Depending on the outcome, this program
# example will either be 'True' or False'. Type and execute/run this program example
# and change the values of 'gate1=True', and 'gate2=False' to different 'True' and
# 'False' combinations, for example: 'gate1=False, 'gate2=False.
gate1=True
gate2=False
if gate1 and gate2:
print(f' "AND" is true because gate1 is "{gate1}" and gate2 is "{gate2}".')
elif gate1 or gate2:
print(f' "OR" is true because gate1 is "{gate1}" and gate2 is "{gate2}".')
else:
print(f' "AND" is true because gate1 is "{gate1}" and gate2 is "{gate2}".')
'''-----------------------------------------------------------------------------'''
# Type and execute/run The George Boole Game program example to get a better
# understanding of how Boolean Logic works and why it works.Type different "True"
# and "False" combinations and see what Gearoge Boole does. Press 'Q' to quit
# playing.
print('\nThe George Boole Game\n')
print('George loves to play "True" or "False",\nbut he needs your help to play it.')
print('\nPress "Q" to quit!')
while True:
Boole1=input('\nType "True" or "False" for the first value. ').strip()
if Boole1=='q':
print('Thanks for playing!')
break
print(f'\nYou chose "{Boole1.title()}" for your first value.\n')
Boole2=input('\nType "True" or "False" for the second value. ').strip()
if Boole2=='q':
print('Thanks for playing!')
break
print(f'You chose "{Boole2.title()}" for your second value.\n')
if Boole1=='true' and Boole2=='true':
print(f' "AND" is true because Boole1 is "{Boole1.title()}" \
and Bool2 is "{Boole2.title()}".\n')
elif Boole1=='false' and Boole2=='false':
print(f' "AND" is true because Boole1 is "{Boole1.title()}" and Boole2 is \
"{Boole2.title()}".\n')
elif Boole1=='true' or Boole2=='true':
print(f' "OR" is true because Boole1 is "{Boole1.title()}" and Boole2 is \
"{Boole2.title()}".\n')
elif Boole1=='false' or Boole2=='false':
print(f' "OR" is true because Boole1 is "{Boole1.title()}" and Boole2 is \
"{Boole2.title()}".\n')
else:
print('Type the right vales please!')
'''-----------------------------------------------------------------------------'''
# This little flip flop game is a great example of how the conditional while-loop works.
# The 'else' statement executes/runs when the user types the wrong keys, and the
# while-loop iterates/repeats over again while ignoring the 'break' statement.
print('\nWelcome to Flip! Flop!')
print('\nPlease type the words "flip" or "flop", then press (ENTER)')
print('\nWhen you give up, press (ENTER) to quit playing Flip! Flop!')
while True:
flip=input('\nFlip? or Flop? ').strip()
if flip=='flip':
print('\nFlop!')
elif flip=='flop':
print('\nFlip!')
elif flip=='':
print('\nThanks for playing Flip! Flop!')
break
else:
print('\nYou can\'t cheat now! Do you flip? or do you flop?')
input('\nEnd of program. Press Enter to quit.')
'''-----------------------------------------------------------------------------'''
# The 'not' Operator
'''
The 'not' operator simply turns true values into false values, and visa versa; false \
values into true values. For example, true 'and' true is 'True' and false 'and' false is \
'False'. Now here is where things get a little bit weird when implementing a 'not' \
operator against true and false values. Here are some examples of the 'not' operator \
and how it turns true values into false values, and false values into true values.
'''
# Here are some examples of how the 'not' operator works. Type and execute/run
# each of these program examples to see how they work.
Boole=True
if Boole==True:
print(Boole)
print('George Boole says \'True\' because \'True\' and \'True\' are true.')
Boole=False
if Boole==False:
print(Boole)
print('George Boole says \'False\' because \'False\' and \'False\' are false.')
'''-----------------------------------------------------------------------------'''
# Here are two examples of how 'True' and 'False' values contradict one another,
# which causes these two program examples below not to show any output on the
# screen, except the 'print' statement, that says George Boole does not contradict
# himself.
Boole=True
if Boole==False:
print(Boole)
print('George Boole does not contradict himself.')
Boole=False
if Boole==True:
print(Boole)
print('George Boole does not contradict himself.')
# Remeber that if 'True' equals 'True', then the outcome will also equal 'True'.
# However, when a 'not' operator is impemented into the mix of a 'True equals 'True'
# comparison, that's when things get strangely confusing. However, the confusion is
# nothing to fret about. The 'not' operator simply turns a 'True' equals 'True' into a
# 'True' equals 'False', which acts like the program examples under the following
# program example. All three program examples won't show any output on the
# screen, but the 'print' statement 'George Boole does not contradict himself.'
Boole=True
if Boole==True:
if not Boole:
print(Boole)
print('George Boole does not contradict himself.')
Boole=True
if Boole==False:
print(Boole)
print('George Boole does not contradict himself.')
Boole=False
if Boole==True:
print(Boole)
print('George Boole does not contradict himself.')
'''-----------------------------------------------------------------------------'''
# These two program examples below show how a 'not' operator makes a true value
# become a false value, and how a false value becomes a true value.
variable=True
if not variable:
print('False becomes True')
else:
print('True Becomes False')
variable=False
if not variable:
print('False becomes True')
else:
print('True Becomes False')
'''-----------------------------------------------------------------------------'''
# Here are some more examples of 'True' and 'False' conditionals. Type and
# execute/run each of these program examples below and see what happens when
# 'True' and 'False' values become 'False' and 'True' values instead.
Boole=True
if Boole==True:
print('True')
else:
Boole==False
print('False')
Boole=False
if Boole==True:
print('True')
else:
Boole==False
print('False')
'''-----------------------------------------------------------------------------'''
# Here are some more examples of the 'not' operator. Type and execute/run each of
# these program examples below and see what happens when a 'True' value is 'not'
# 'True' and when a 'False' value is 'not' 'False'.
Boole=True
if not Boole==True:
print('True')
else:
Boole==False
print('False')
Boole=False
if not Boole==True:
print('True')
else:
Boole==False
print('False')