More README Work

This commit is contained in:
2022-12-17 16:15:53 -05:00
parent 05392f945b
commit 0f49a82db7
9 changed files with 237 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
# 4 - If/Else and Logical Operators - Challenge
Build a program that requests the user's age, and return one of the following:
- <= 18 - You're a youngin'
- > 18 AND <= 35 - The early adult years

View File

@@ -12,26 +12,31 @@ else:
We can check different things to make different choices, neat. But what do we do if we need to ask a bunch of related questions in our program? That's were elif comes in.
```
if (some condition):
(DO STUFF IF CONDITION IS TRUE)
elif (some other condition):
(DO STUFF IF SOME OTHER CONDITION IS TRUE)
else:
(DO STUFF IF ALL CONDITIONS (QUESTIONS) ARE FALSE)
```
elif allows use to specify another condition and some code to go with it to add another question to ask if the questions that came before it were false. This gives us more options to determine what we're going to do next.
## Do I need to use if/elif/else all the time?
The smallest form of if is this
```
if (some condition):
(DO STUFF IF CONDITION IS TRUE)
```
You don't need else, you don't need elif, unless you want them and it makes sense for what you're trying to do in your program
## Does the indentation matter?
YES! Python is ULTRA PICKY about indentation. In general, when you see a colon (:) in Python, that means that some portion of the code underneath it should be indented one tab more than the current level of indentation. Here's a very bland example (ignore the for loop for the moment, we'll talk about that next lesson)
```
for x in range(10):
if x == 5 or x == 7:
print("Potato")
@@ -39,6 +44,7 @@ for x in range(10):
print("Grape")
else:
print("You get nothing")
```
The identation determines something called scope. Scope is essentially defining blocks of code that "live in the same space". Scope can be a complicated topic, so we're going to gloss over it. [W3Schools](https://www.w3schools.com/python/python_scope.asp) does a really good job explaining scope relatively quickly. You can even try the code for yourself right on the webpage!
@@ -57,7 +63,7 @@ Sometimes conditions are as simple as, is this less than that, or is this equal
AND will result in a True value only when both the left and the right side of the AND are True.
Example: intValue < 100 and intValue > 50 is only True when intValue is any number between 51 and 99
Example: `intValue < 100 and intValue > 50` is only True when intValue is any number between 51 and 99
The Truth Table for AND looks like this
<table>
@@ -86,4 +92,56 @@ The Truth Table for AND looks like this
<td>False</td>
<td>False</td>
</tr>
</table>
</table>
OR will result in a True value so long as at least one side (the left, the right, or both) is True, OR is only False when both sides are False
Example: `intValue < 0 or intValue > 100` is True when intValue is less than 0 (any negative number) OR intValue is a value of 101 or greater
The Truth Table for OR looks like this
<table>
<tr>
<th>Left Side</th>
<th>Right Side</th>
<th>Result</th>
</tr>
<tr>
<td>True</td>
<td>True</td>
<td>True</td>
</tr>
<tr>
<td>True</td>
<td>False</td>
<td>True</td>
</tr>
<tr>
<td>False</td>
<td>True</td>
<td>True</td>
</tr>
<tr>
<td>False</td>
<td>False</td>
<td>False</td>
</tr>
</table>
One other Logical Operator that doesn't really combine two values is NOT. NOT flips whatever the resulting value is, True becomes False, False becomes True.
Example: `not intValue < 100` is True when intValue has a value of 100 or greater
NOT can be a little weird to think about, adding negation to logic can make things a little fuzzy to think about. An actual Python Programming course would cover this more than I can here, but there is one thing to keep in mind when working with logical operations. Like PEMDAS from math, logical operators also have an order of operations, see the formatted list below
- Parenthesis
- NOT
- AND
- OR
Parenthesis is at the top, whatever you see in parenthesis will happen first, NOT is evaluated next, then AND, and finally OR. That means if you have something like this:
`someValue or someOtherValue and thisValue`
The AND of someOtherValue and ThisValue will happen first, because AND has higher precedence, the result of that will then be combined with OR only after the AND operation has completed. If you wanted OR to come first, you'd have to write it something like this
`(someValue or someOtherValue) and thisValue`