Conditional statements allow your code to branch in different directions.
The decision is based on a logical test which results in True or False.
if test:
do this
x = 5
y = 4
if x > y:
print('X is greater than Y')
What happened? The conditional test 'is x > y' was true, so the print() occurred.
x = 4
y = 4
if x > y:
print('X is greater than Y')
What happened? It appears that 4 cannot be less than 4! The print() will not happen.
x = 5
y = 4
if x > y:
print('X is greater than Y')
if y == 4:
print('Y is equal to 4')
Keep in mind that those 2 if statements are not aware of each other. Later on in this lesson, I'll show you how to make those two statements aware of each other.
x = 5
y = 4
if (x > 4) and (y > 4):
print('Both are greater than 4')
if (x > 4) or (y > 4):
print('One or both are greater than 4')
In the first example, and is being used to evaluate both tests. Please look at the logic gate note if you are unfamiliar with and.
In the second example, or is being used to evaluate both tests. Please look at the logic gate note if you are unfamiliar with or.
Is there not, a third ?
if test:
do this
else:
do this instead
Notice how in the if / else, the else does not need a condition. It will just run when the if condition fails.
x = 5
y = 4
if (x > 4) and (y > 4):
print('Both are greater than 4')
else:
print('Both are NOT greater than 4')
The practice of pairing an else with an if is HIGHLY advisable. It does make debugging easier. If you find yourself writing an if statement, and you don't see the point of adding an else to it, add an else anyhow. In that else, insert a print statement like "This should not happen #1". I added the number because if it ever does happen, and you have many of them in your code, you'll appreciate knowing where it occurred.
Earlier we saw that a series of if statements did not know about each other. elif (else-if) allows a series of if statements to know about each other.
if condition_1:
do ONLY this, if the first condition is true
elif condition_2:
do ONLY this, if the second condition is true
elif condition_3:
do ONLY this, if the third condition is true
else:
if everything failed, then ONLY do this
In this example, which statement will print?
x = 3
if x == 1:
print('x is 1')
elif x == 2:
print('x is 2')
elif x == 3:
print('x is 3')
else:
print('x is not valid')
Fundamentally, documentation is the most effective way to maintain of your source code, explain your binary file, and keep users updated.
The 3 Levels of Documentation are:
Source code comments
Other programmers / code maintainers / people who will work on the project when you are gone / project leader / Teacher
In the source.
Comment as you code, use clear variable names (this makes your commenting easier). Algorithms and complex decision structures should be well explained. Files require a document header
Help for users while program is running. The Help function in a menu (or button) should have help located with the binary file ... today, most help functions point to a website, wiki, or forum ... why? Information can be updated centrally.
Users / Teacher
Appears in the running application.
Consider a menu option. Example:
S - Start
H - Help
Q - Quit
Help for users outside of the program.
Think 'Reference Manual'.
Users / Teacher
In a text (.txt) file named assn1_readme.txt or assn1_help.txt
Use any text editor, not a word processor to build the file. Use a horizontal divider (78 chars max) to avoid word-wrap.
Here are some headings for your external documentation:
Author - Your name
Class / Section - Your course code and section number
Date - The hand in date
Version - How do you keep track of what version of your software you are working on?
Unit / Question # - What Unit does the assignment belong to? Which question was it?
Programming Language - Be specific: Python 3.x or Python 3.4.4. Both are better than indicating just Python
Problem Description - Your description of my question. This is just like the header comments.
Program Assumptions - What is required for your program to run? A computer running which OS, and which version of your language? Are there any other assumptions that you are making on behalf of your user?
Features of Program - What new features did you add to my question?
Restrictions - What your solution cannot do. THIS IS NOT A KNOWN ERROR. Please see next category.
Known Errors - List your known errors. These are mistakes. errors that cause it to crash, logical, syntax or run-time errors.
Implementation Details - Pretend a friend wants to try to use a program you wrote.
(Thanks to Sarah J. for the clarification)
Additional Files - What files should be included with your program?