Skip to content

Commit 969c6d2

Browse files
committed
Added conditional
1 parent ffd6985 commit 969c6d2

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

docs/trinket/05-conditionals.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,39 @@ There are two things we do with equations in Python. The first is to use the eq
88
Here is the basic syntax of the Python conditional operator.
99

1010
```py
11-
if (i > 3):
12-
# do something if i is greater than 3
11+
if (i > 2):
12+
# do something if i is greater than 2
1313
else:
14-
# do something else when i is exactly 3 or less than 3
14+
# do something else when i is exactly 2 or less than 2
1515
```
1616

17+
## Simple Conditionals
18+
Here is a program that
19+
20+
```py
21+
import turtle
22+
dan = turtle.Turtle()
23+
dan.shape('turtle')
24+
25+
distance = 100
26+
angle = 90
27+
28+
for i in range(1, 5):
29+
# i modulo 2 is the remainer after we divide by 2
30+
dan.write(i, font=("arial", 16, "normal"))
31+
if i > 2: # true if i greater than 2
32+
dan.color('red')
33+
dan.pensize(5)
34+
else: # if i is exactly 2 or less than 2
35+
dan.color('blue')
36+
dan.pensize(3)
37+
dan.forward(distance)
38+
dan.right(angle)
39+
```
40+
41+
[Link to Trinket with Simple Conditional](https://trinket.io/python/db5978a312)
42+
43+
## Changing Odd and Even Edge Colors
1744
We would like every other side to change color. To do this we will add an if-then-else block of code to our example program. This block of code is called a conditional block. The condition is an expression that evaluates to be either TRUE for FALSE. In our example we will test to see if the index of our loop (the letter "i") is ODD or EVEN. We can do this by looking at the remainder after we divide by 2. Python has a handy operator called the modulo operator that uses the percent character which is above the number five on your keyboard. The test for ODD or EVEN is this:
1845

1946
```py

0 commit comments

Comments
 (0)