HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
if-else
Deutsch   English   

9. IF-ELSE

 

 

YOU LEARN HERE...

 

how to proceed so that program blocks are only executed under certain conditions. You will also learn how to negate conditions and how to combine them with AND and OR operators.

 

 

EXAMPLES

 

In your program, use the randint(-250, 250) command to generate two random numbers x and y and let the turtle jump to the position (x, y). Draw a red dot there if the turtle is inside the circle with a radius of 250, otherwise draw a gray dot. Repeat this process 10,000 times. Sometimes this is also referred to as random rain.

If the turtle is inside the circle, Pythagoras' theorem states that its distance squared rsquare to the origin is less than 250 * 250 = 62500.

You can say:

“If the square of the radius is less than 62500, draw a red dot, otherwise draw a gray dot.”

You express this with the keyword if condition: and indent the following program block. If the condition is not true, the program block after else: .
Don't forget the colon after if and after else.!

 


Program:    

from gturtle import *
from random import randint

makeTurtle()
hideTurtle()

repeat 10000:
    x = randint(-250, 250)
    y = randint(-250, 250)
    setPos(x, y)
    rsquare = x * x + y * y
    if rsquare < 62500:
       setPenColor("red")
       dot(4)
    else:
       setPenColor("gray")
       dot(4)
► Copy to clipboard

 

 

REMEMBER...

 

Instead of saying that a condition is fulfilled or not fulfilled, you can also say that the condition is true or false. If you don't want the gray dots to be drawn, you can also leave out the else part. Try it!

If you want to cancel the program prematurely, click on the red square button.

 

 

LINK CONDITIONS WITH AND

 

As is common in colloquial language, you can combine two conditions with and. The condition combined in this way is only true if both partial conditions are true. Here you draw red dots if the x-coordinate of the turtle is greater than -100 and less than 100. The result is obviously a vertical red stripe.

 

Program:    

from gturtle import *
from random import randint

makeTurtle()
hideTurtle()

repeat 10000:
    x = randint(-250,250)
    y = randint(-250,250)
    setPos(x, y)
    if x > -100 and x < 100:
       setPenColor("red")
       dot(4)
    else:
       setPenColor("gray")
       dot(4)
► Copy to clipboard

 

 

LINK CONDITIONS WITH OR

 

A horizontal red stripe satisfies the condition y > -100 and y < 100. Points that lie in either the vertical or horizontal or both areas satisfy both conditions. Colloquially, you say that they lie in the vertical or (also) horizontal stripe. You link the two conditions with or.

 

Program:    

from gturtle import *
from random import randint

makeTurtle()
hideTurtle()

repeat 10000:
    x = randint(-250,250)
    y = randint(-250,250)
    setPos(x, y)
    if (x > -50 and x < 50) or (y > -50 and y < 50):
       setPenColor("red")
       dot(4)
    else:
       setPenColor("gray")
       dot(4)
► Copy to clipboarderen

 

 

NEGATE CONDITION

 

You can use the keyword not to negate a condition, Points that are not in the vertical strip satisfy the condition not (x > -100 and x < 100), as the following program shows:

 

Program:    

from gturtle import *
from random import randint

makeTurtle()
hideTurtle()

repeat10000:
    x = randint(-250, 250)
    y = randint(-250, 250)
    setPos(x, y)
    if not (x > -100 and x < 100):
       setPenColor("red")
       dot(4)
    else:
       setPenColor("gray")
       dot(4)
► Copy to clipboard

 

 

REMEMBER...

 

The correct use of parentheses in conditions is very important. Since not binds more strongly than and and or, you have to put a bracket here. The weakest binding is or.

Logically, you also get the gray stripe if you require that x <= -50 or x >= 50. So you can formulate conditions in different ways. Give it a try!

The following comparison operators are available for numbers:

  <   less
  <=   less than or equal to
  ==   equal
  >=   greater than or equal to
  >   greater
  !=   different

In particular, you need to get used to the doubling of the equals sign in the equality condition. This is necessary so that the computer can distinguish between the assignment and the equality condition.

 

 

 

MULTIPLE SELECTION

 

To distinguish between more than two cases, you have to insert another if condition in the else part. In Python, you can use the shorter elif and write:

 
if bed1:
    ... 
elif bed2:
    ...
else:
    ...

You can see this in the following sample example of how one of 5 pen colors is selected at random.

 

Program:    

from gturtle import *
from random import randint

makeTurtle()
hideTurtle()
n = randint(1, 6)

if n == 1:
    setPenColor("red")
elif n == 2:
    setPenColor("yellow")
elif n == 3:
    setPenColor("magenta")
elif n == 4:
    setPenColor("green") 
elif n == 5:
    setPenColor("blue")        
else:
    setPenColor("black") 
dot(100)
  
► Copy to clipboard

 

 

TO SOLVE BY YOURSELF

 
1.

Use random numbers between -150 and 150 to get the following graphs.

a)

b)

 

2.

You have learned that you can draw a spiral with the following program.

from gturtle import *
makeTurtle()
hideTurtle()

s = 10
repeat 100:
    forward(s)
    right(90)
    s = s + 2

Use the if-else structure to draw a two-color spiral.

 

 
 
3.

Use the multiple selection to draw a four-color spiral.