You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/trinket/05-conditionals.md
+30-3Lines changed: 30 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,39 @@ There are two things we do with equations in Python. The first is to use the eq
8
8
Here is the basic syntax of the Python conditional operator.
9
9
10
10
```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
13
13
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
15
15
```
16
16
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 inrange(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
17
44
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:
0 commit comments